Matlab App Desinger: How to update a figure with button

32 views (last 30 days)
I want to create multiple plots in an app and later update the plots when new data is added.
In one step the user can add data (in this case a trajectory) and plot a preview. Later on the user can add another data (a single coordinate inside the trajectory) and I want this new point to be plotted inside the wirst figure window. Right now I'm having the issue that App Desinger always opens a new window.
Here's a simplified version of my code so far:
app.counter = 0;
function FirstButtonPushed(app, event)
% check if user selected a file yet
position = importfile1(filename); % imports table with Lat and Lon
% plot the preview
fig_trajectory = uifigure('Name', 'Trajectory');
hold on;
plot(position.Longitude, position.Latitude);
% check if user has skipped this step and already added the new additional data
if app.counter >= 1
plot(app.vpos(:,2), app.vpos(:,1), '*')
function SecondButtonPushed(app, event)
app.pos = [48.0000 11.0000; 48.0001 11.000];
app.counter = app.counter +1;
% call previous plot
% add data
plot(app.pos(:,2), app.pos(:,1), '*')
How can I tell Matlab to update the figure?
Thanks in Regards!

Accepted Answer

Mario Malic
Mario Malic on 5 Feb 2021
the best way to add/modify the plot data is by XData, YData, ZData, or XDataSource, ... properties. Probably, by changing their properties, callback to automatically update the plot will be executed and there won't be need for refresh button.
In AppDesigner, you need to provide the handle to the UIAxes component you're plotting on, then it won't open the new figure.
x = 1;
y = 1;
h = plot(app.UIAxes, x, y); % saving the handle h of the line object,
If you want to change the data (this is useful if you have a lot of points in your graph), here's an example
xNew = [x, 2];
yNew = [y, 3];
set(h, 'XData', xNew, 'YData', yNew);
if not, then you can use the plot function like shown above.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by