HOW TO PLOT ON THE SAME FIGURE PLOTS OF DIFFERENT SCRIPTS

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)?

4 Comments

  1. You should learn to write functions instead of scripts;
  2. Use hold on
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
Run the first script. Then
set(findobj(groot, 'axes'), 'NextPlot', 'add');
Now run the second script.
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

Sign in to comment.

Answers (1)

Hi Pasquale,
You can plot two graphs with same variables and parameters in the same figure by using functions to encapsulate the code in each program which will ensure that the variables will be local to each function and won't interfere with each other. You can refer to the below example steps for better understanding:
  • Let's create a file named "program1.m" with the variables 'x' and 'y'.
% Your code for the first program
x = linspace(0, 10, 100);
y = sin(x);
% Save the variables to a .mat file
save('program1_data.mat', 'x', 'y');
  • Let's create another file named "program2.m" with the same variables 'x' and 'y'
% Your code for the second program
x = linspace(0, 10, 100);
y = cos(x);
% Save the variables to a .mat file
save('program2_data.mat', 'x', 'y');
  • In the main script call the two files and record the variables. Now plot the graphs in the same figure.
% Clear workspace and close all figures
clear;
close all;
% Run the first program and save its data
run('program1.m');
% Load the data from the first program
data1 = load('program1_data.mat');
% Run the second program and save its data
run('program2.m');
% Load the data from the second program
data2 = load('program2_data.mat');
% Plot the results on the same figure
figure;
plot(data1.x, data1.y, 'r-', 'DisplayName', 'Program 1');
hold on;
plot(data2.x, data2.y, 'b--', 'DisplayName', 'Program 2');
hold off;
% Add labels and legend
xlabel('X-axis');
ylabel('Y-axis');
legend show;
title('Comparison of Program 1 and Program 2');
Output:
You may refer to the following MathWorks documentation links to have a better understanding on combining multiple plots:
  1. https://www.mathworks.com/help/matlab/ref/hold.html
  2. https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 14 Nov 2023

Answered:

on 8 Aug 2024

Community Treasure Hunt

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

Start Hunting!