- if any figure windows are being opened in the background. Sometimes MATLAB shows plot figures in the background, and they may not be immediately visible.
- if you have any figure windows already open prior to running the script. MATLAB may reuse existing figures for subsequent plots, which could cause confusion when checking the generated figures.
Figures generated in Live Script are displayed as old plots with same figure(n)
93 views (last 30 days)
Show older comments
I have a live script which creates graphs from several vectors. It generates 3 plots, called figure(1), figure(2) and figure(3). It used to work just fine, and generate 3 figures, which were displayed next to the script.
However, today I tried inputing a different set of vectors, and soon problems appeared:
- Upon running the script, variables are created and stored in the workspace as usual, but no figures appear. Only when running it 3 or 4 times figures appear.
- Some of the figures are transparent, when they generate at all
- Often, figures from previous data sets are displayed, only when the figure is opened in a window can the actual result be seen
Weirdly, other scripts are not affected by this problem
Any help is appreciated
clear
Data=tdmsread("C:\Users\f\data1.tdms",ChannelGroupName="AI",ChannelNames=["Time (us)","P_LO_VT2","P_F_VT2","M_FLOW_LO"]);
Table=Data{1,1};
Array=table2array(Table);
% Make Vector
t=Array(:,1); %Time (us)
P2=Array(:,2);
P1=Array(:,3);
M=Array(:,4);
% Smooth and convert time to s
SmoothNumber=1000;
t=((t-t(1,1))/1e6);
P1_smooth=movmean(P1,SmoothNumber);
P2_smooth=movmean(P2,SmoothNumber);
M_smooth=movmean(M,SmoothNumber)*1000;
% Plot
figure(1);
plot(t,P1_smooth);
title('P1');
xlabel('Time [s]');
ylabel('P1 [bar]');
grid on;
figure(2)
plot(M_smooth,P2_smooth)
hold on
scatter(52.5,41.8,'filled','r')
title('Flow');
xlabel('Flow [g/s]');
ylabel('P2 [bar]');
grid on
hold off
figure(3)
plot(t,P2_smooth,'b')
title('P2 and M');
xlabel('Time [s]');
ylabel('P2 [bar]');
yyaxis right
plot(t,M_smooth,'r')
ylabel('Mass flow [g/s]');
grid on
3 Comments
RIchard
on 4 Jan 2024
Edited: Walter Roberson
on 4 Jan 2024
I believe it is an problem with Live Script. Looking through all the different documentation online, I fail to find a single occurance where a Live Script has 2 y-axises. I also have this problem and recreated it with the following code:
a= 1:10;
b=rand(10,1);
c=rand(10,1)+5;
plot(a,b,a,c)
yyaxis left
plot(a,b)
yyaxis right
plot(a,c)
I cleared all outputs, closed and reopened matlab. At that point, I ran the above code once in a live script. Below is the screenshot. Note that both figures have two sets of y-axis even though only the 2nd plot should have them.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1582756/image.png)
Answers (1)
Sulaymon Eshkabilov
on 4 Jan 2024
Using figure command between the plots solves this ambiguity.
a= 1:10;
b=rand(10,1);
c=rand(10,1)+5;
figure
plot(a,b,a,c)
figure
yyaxis left
plot(a,b)
yyaxis right
plot(a,c)
%% As printscreen shows here
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1582821/image.jpeg)
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!