Create new Figures with MATLAB live script

조회 수: 67 (최근 30일)
Ahmad
Ahmad 2024년 2월 23일
댓글: Ahmad 2024년 2월 26일
After using MATLAB live script, I noticed that when I create figures, the numbering does not increment by 1 as expected. Instead, it skips figures 2 to 4. For example, if I write the code in live script:
figure
figure
figure
the output will be figures 1, 5, and 6. How can I resolve this problem?

채택된 답변

Austin M. Weber
Austin M. Weber 2024년 2월 24일
편집: Austin M. Weber 2024년 2월 24일
Hi @Ahmad,
I recommend putting the following command:
close all
at the beginning of your MATLAB live script. This should reset the figure numbers before the rest of the script is run. Alternatively, you can manually assign numbers to your figures however you like:
figure(1)
figure(2)
figure(57)
  댓글 수: 3
Austin M. Weber
Austin M. Weber 2024년 2월 25일
I have come across the same issue in the past where I would execute a block of code in an .mlx file and then the plots would get combined. The solution that I have found for this is to separate the figures into separate blocks using a section break (%%):
hfig = figure;
Num=hfig.Number;
for var3=1:numel(d)
fig1=figure(Num);
hold on
plot(VTh,reshape(WsMax(:,1,var3),1,[]))
title('plot 1');
xlabel('VTh');
ylabel('Ws');
hold off
end
%% Start new section here
for var3 = 1:numel(d)
fig2=figure(Num+1);
hold on
plot(VDC,reshape(WsMax(1,:,var3),1,[]))
title('plot 2');
xlabel('VDC');
ylabel('Ws');
hold off
end
Note that in .mlx files you might run into problems trying to execute a for-loop across multiple sections, so I split your code into two for-loops. You can adjust this however you need. Hopefully you can get your script to work now.
Alternatively, you can try running your code in a normal script file (.m) instead of a live script file.
Ahmad
Ahmad 2024년 2월 26일
Initially, the .mlx file was working fine, but later on, it only displayed one figure while the other was missing. however, everything runs smoothly in the .m file.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying 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