To build a data visualization app using MATLAB App Designer
To build a data visualization app using MATLAB App Designer, follow these steps:
Step 1: Launch App Designer
Open MATLAB and type appdesigner
in the command window. Create a new app using the “Blank App” template.
Step 2: Design the User Interface
- Add UI Components:
- Axes: For displaying plots (named
UIAxes
by default). - Buttons:
- “Load Data” button (
LoadDataButton
). - “Plot Data” button (
PlotDataButton
).
- “Load Data” button (
- Drop-Down Menus:
- X-axis selector (
XDropDown
). - Y-axis selector (
YDropDown
). - Plot type selector (
PlotTypeDropDown
).
- X-axis selector (
- Labels: For clarity.
- Axes: For displaying plots (named
- Arrange Components:
- Drag and position components as shown below:
[Load Data Button] [X: DropDown] [Y: DropDown] [Plot Type: DropDown] [Plot Data Button] [UIAxes]
- Drag and position components as shown below:
Step 3: Define App Properties
In the Code View, add properties to store data:
properties (Access = private) Data table % Store loaded data Variables cell % Column names from data end
Step 4: Implement Callbacks
A. Load Data Button Callback
function LoadDataButtonPushed(app, event) % Open file dialog [filename, pathname] = uigetfile('*.csv'); if filename == 0 return; % User canceled end % Read CSV data app.Data = readtable(fullfile(pathname, filename)); % Extract variable names app.Variables = app.Data.Properties.VariableNames; % Update dropdowns app.XDropDown.Items = app.Variables; app.YDropDown.Items = app.Variables; end
B. Plot Data Button Callback
function PlotDataButtonPushed(app, event) % Check if data is loaded if isempty(app.Data) uialert(app.UIFigure, 'Load data first!', 'Error'); return; end % Get selected options xVar = app.XDropDown.Value; yVar = app.YDropDown.Value; plotType = app.PlotTypeDropDown.Value; % Generate plot cla(app.UIAxes); switch plotType case 'Line' plot(app.UIAxes, app.Data.(xVar), app.Data.(yVar)); case 'Scatter' scatter(app.UIAxes, app.Data.(xVar), app.Data.(yVar)); case 'Bar' bar(app.UIAxes, app.Data.(xVar), app.Data.(yVar)); end xlabel(app.UIAxes, xVar); ylabel(app.UIAxes, yVar); end
Step 5: Configure Drop-Down Menus
- Set
PlotTypeDropDown
items to{'Line', 'Scatter', 'Bar'}
in the Design View.
Step 6: Test the App
- Run the App: Click “Run” (green arrow) in App Designer.
- Load Data: Click “Load Data” and select a CSV file (example below).
- Select Variables: Choose X/Y variables and plot type.
- Plot: Click “Plot Data”.
Example CSV Data (sample.csv
):
Time,Voltage 0,1.2 1,3.4 2,5.6 3,7.8
Key Considerations:
- Error Handling: Add checks for non-numeric data or empty selections.
- Enhancements:
- Add 3D plots or histograms.
- Include data filtering/preprocessing.
- Export plots as images.
Final Code Structure:
- App Designer File: Save as
DataVizApp.mlapp
. - CSV Data: Ensure columns are numeric for plotting.
This app provides a basic framework for data visualization, which you can extend based on specific needs