HOW TO PLOT ON THE SAME FIGURE PLOTS OF DIFFERENT SCRIPTS

4 ビュー (過去 30 日間)
Pasquale Varlotta
Pasquale Varlotta 2023 年 11 月 14 日
回答済み: Abhas 2024 年 8 月 8 日
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 件のコメント
Walter Roberson
Walter Roberson 2023 年 11 月 14 日
Run the first script. Then
set(findobj(groot, 'axes'), 'NextPlot', 'add');
Now run the second script.
Pasquale Varlotta
Pasquale Varlotta 2023 年 11 月 14 日
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

サインインしてコメントする。

回答 (1 件)

Abhas
Abhas 2024 年 8 月 8 日
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

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by