This topic has been permanently closed and transferred to MATLAB Answers.

General
Follow


Pasquale Varlotta

HOW TO PLOT ON THE SAME FIGURE PLOTS OF DIFFERENT SCRIPTS

Pasquale Varlotta on 14 Nov 2023
Latest activity Edit by Pasquale Varlotta on 14 Nov 2023

Hi, I'm in truble because I have two programs with the same variables and parameters. The main of the study is to change a value and plot the results. The problem is that I want them on the same plot but I use the same name for the variabes in the two different programs so when I use some function to join the figures togheter matlab resets the values obtained in the first program and runs only the second one.
Is there a method to avoid changing all the names of the variables in one of the two programs (because they have something like 500 lines)?
Walter Roberson
Walter Roberson on 14 Nov 2023
  1. You should learn to write functions instead of scripts;
  2. Use hold on
Pasquale Varlotta
Pasquale Varlotta on 14 Nov 2023 (Edited on 14 Nov 2023)
Sorry could you explain it better?
I know how to use hold on the problem is that the figures to plot have the same variables on x and y axis so if i write something like:
hold on
figure(1)
figure (2) (I don't know if is correct but it's just an exemple)
matlab plots only the second one
Walter Roberson
Walter Roberson on 14 Nov 2023
Run the first script. Then
set(findobj(groot, 'axes'), 'NextPlot', 'add');
Now run the second script.
Pasquale Varlotta
Pasquale Varlotta on 14 Nov 2023 (Edited on 14 Nov 2023)
Do you mean run the first script an then use set(findobj(groot, 'axes'), 'NextPlot', 'add'); in the command window?
Because I keep having this error:
Error using matlab.ui.Root/findobj
Incomplete parameter-value pair.
Maybe I use the function wrongly
After that I tryed like that:
program_1
set(findobj(groot, 'Type', 'axes'), 'NextPlot', 'add');
program_2
This solve the error but matlab still plots just the last one graph

See Also