Multiple plots on same Axes (choosing from listbox) in app designer

3 views (last 30 days)
I am trying to have multiple plots on same axis in app designer through selecting the varibale of intrest on Y axis from Listbox and displaying one plot at a time while clearing the data from the previous plots. The proble is, everytime I plot, it returns with one plot with variable selected first and doesn't work for the rest of the variables
Example for 2 pvariables is as under !
function PlotButtonPushed(app, event)
xinput = app.XAxisListBox_3.Value;
yinput = app.YAxisListBox.Value;
xdata = app.thj(:,1);
legend(app.UIAxes);
cla(app.UIAxes);
hold(app.UIAxes,'on');
%SO2 Plot
if strcmp(xinput, 'Time')
ydata = app.Var(:,1);
plot(app.UIAxes,xdata,ydata,'-','DisplayName',app.YAxisListBox.Value);
xlabel(app.UIAxes,app.XAxisListBox_3.Value);
ylabel(app.UIAxes,app.YAxisListBox.Value);
end
%XCB plot
if strcmp(xinput, 'Time')
ydata = app.Var(:,2);
plot(app.UIAxes,xdata,ydata,'-','DisplayName',app.YAxisListBox.Value)
xlabel(app.UIAxes,app.XAxisListBox_3.Value);
ylabel(app.UIAxes,app.YAxisListBox.Value);
end
end
% Button pushed function: ClearButton
function ClearButtonPushed(app, event)
cla(app.UIAxes);
end

Answers (1)

Tridib
Tridib on 24 Apr 2025
I understand that you want to display several plots on the same axis, but only one plot should be visible at a time. The plot shown will depend on which variable is selected for the y-axis from the listbox of available variables.
But, you are plotting the data twice because both ‘if strcmp(xinput, ‘Time’)’ blocks will always be true when ‘xinput’ is ‘Time’. Additionally, you are not using the ‘Y’ variable selected in the ‘ListBox’ to decide which column to plot on the Y-axis. Since your plotting logic is the same in both cases, you do not need two separate ‘if’ blocks; you can combine them and make the code more efficient and clear.
  • Assume that ‘app.Var’ holds your ‘Y’ data, with each column representing a different variable, and ‘app.YAxisListBox.Items’ contains the names of these variables, such as 'SO2' or 'XCB'. The index of the variable selected in the ‘ListBox’ tells you which column from ‘app.Var’ you should plot on the Y-axis.
  • Add the necessary buttons, axes, and listboxes, and give each one a suitable name. Update the items and values in both listboxes as needed.
  • In the code view section, add the properties ‘thj’ and ‘Var’ by selecting the add property option under the editor tab.
  • Add the following code in the startupFcn callback function, as you need to set data for these properties before using them in your callbacks:
% Example data initialization
app.thj = (1:100)'; % 100 time points
app.Var = [rand(100,1), rand(100,1)]; % Two variables to plot
app.YAxisListBox.Items = {'SO2', 'XCB'}; % Variable names
app.XAxisListBox_3.Items = {'Time'}; % X axis name
  • Add the following code for the PlotButton:
% Getting selected X and Y variable names
xinput = app.XAxisListBox_3.Value;
yinput = app.YAxisListBox.Value;
% Getting X data (Assuming ‘Time’ is the first column)
xdata = app.thj(:,1);
% Finding the index of the selected Y variable
yIndex = find(strcmp(app.YAxisListBox.Items, yinput));
if isempty(yIndex)
uialert(app.UIFigure, 'Selected Y variable not found.', 'Error');
return;
end
% Getting the corresponding Y data
ydata = app.Var(:, yIndex);
% Clearing axes and plotting new data
cla(app.UIAxes);
plot(app.UIAxes, xdata, ydata, '-', 'DisplayName', yinput);
% Labels
xlabel(app.UIAxes, xinput);
ylabel(app.UIAxes, yinput);
% Legend
legend(app.UIAxes, 'show');
Through this, you will be able to achieve the desired functionality.
For more help, refer to the following documentations:
Hope this helps!

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!