Plot two data sets and only clear one of them ?

1 Ansicht (letzte 30 Tage)
Gwendal Marrec
Gwendal Marrec am 17 Mär. 2022
Kommentiert: Gwendal Marrec am 21 Mär. 2022
Hello,
I have a code that plots two data sets on the same plot coming from two different text files using uigetfile(). To do so, I use the function plot twice (because this happens in two separate functions within my app designer).
My problem is that when I load a new text file, I am only able to clear the whole plot or to add the new data set to the existing previous two data sets, instead of just replacing one of them.
I have tried to use hold or linkdata but it doesn't work the way I intend it.
Any help would be much appreciated:)
Gwendal

Akzeptierte Antwort

Voss
Voss am 17 Mär. 2022
You can store the handle(s) to the plotted objects somewhere in your app structure, and delete them and replace them with new plotted objects as needed.
% store the first plotted line as line_1
line_1 = plot(1:10);
hold on
% store the second plotted line as line_2
line_2 = plot(2:11);
% delete line_2
delete(line_2);
% make another line_2 with different data
line_2 = plot(3:12);
line_1 and line_2 here would be stored in your app structure, and they might be created and deleted in different functions within your application.

Weitere Antworten (1)

Adam Danz
Adam Danz am 17 Mär. 2022
Option 1: Delete a data set and add a new data set.
Example:
hold(ax, 'on')
h1 = plot(__);
h2 = plot(__);
% replace h1
delete(h1)
h1 = plot(__);
Option 2: Replace the values of one dataset with the values of the second dataset.
Example:
hold(ax, 'on')
h1 = plot(__);
h2 = plot(__);
% replace h1 data
set(h1, 'XData', x, 'YData', y,)
  1 Kommentar
Gwendal Marrec
Gwendal Marrec am 21 Mär. 2022
Thank you @Adam Danz and @_, both of your answers helped me to understand where my problem was.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by