Clear Filters
Clear Filters

How to save a multiple plots data with labels, title, legends and then plot again on uiaxes?

1 view (last 30 days)
a = 0:0.01:2*pi;
b = sin(a);
c = cos(a);
P1 = plot(app.UIAxes,a,b,'DisplayName','Sin Wave');
xlabel('Time1');
ylabel('Range1');
title('Sin Wave Plot')
P2 = plot(app.UIAxes,a,c,'DisplayName','Cos Wave');
xlabel('Time2');
ylabel('Range2');
title('Cos Wave Plot')
i am running above code in "startup" function, this first draw sinwave and then draw cos wave plot on uiaxes.. now i want to plot P1 of sin wave again on uiaxes with same labels, title and legends . in other words i want to toggle between sin and cos plot through a previous and next button... there are some ways to manually copy axes and children but it is not convinient if i have dozen of plots with multiple legends and data in each plot. Kindly guide any precise way.
there a copyuiaxes function available on fileexchange which may give some desired functionality but i want to do this through in default matlab.

Accepted Answer

Cris LaPierre
Cris LaPierre on 29 Nov 2023
You only have one axes, and both plot commands are to the same axes, so the 2nd plot replaces the first. You must redraw the sin plot if you want it to display.
My approach would be to have the startup function plot one of them, then update the YData of the line to the desired values inside the callback function for your Previous and Next buttons.
Turn variables a, b, c, and P1 into app properties so you can access them in all your callbacks. See here: https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
Note that you must specify the target axes for xlabel, ylabel, and title or they will open a new figure window.
Something like this
properties (Access = private)
a % Description
b
c
P1
end
function startupFcn(app)
app.a = 0:0.01:2*pi;
app.b = sin(app.a);
app.c = cos(app.a);
app.P1 = plot(app.UIAxes,app.a,app.b,'DisplayName','Sin Wave');
xlabel(app.UIAxes,'Time1');
ylabel(app.UIAxes,'Range1');
title(app.UIAxes,'Sin Wave Plot')
end
% Button pushed function: NextButton
function NextButtonPushed(app, event)
app.P1.YData = app.c;
title(app.UIAxes,'Cos Wave Plot')
end
% Button pushed function: PreviousButton
function PreviousButtonPushed(app, event)
app.P1.YData = app.b;
title(app.UIAxes,'Sin Wave Plot')
end

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!