Figures generated in Live Script are displayed as old plots with same figure(n)

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:
  1. 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.
  2. Some of the figures are transparent, when they generate at all
  3. 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

Hello! Can you please share the data file which is causing the issue? That will help us investigate the issue better! Please also check
  • 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.
I recommend trying to run the script in a clean MATLAB session, by restarting it once. This will help you understand if it's a file issue or an environment issue.
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.
I don't have access to the data anymore, but I have had a few other similar experiences with generating figures in live scripts. When the code is copied into the terminal it works flawlessly, but figure generation in the live script is unreliable in the way I described in the inital post.
I'll file a bug report.

Sign in to comment.

Answers (1)

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

Categories

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!