Plotting from a loop in a live script in Matlab 2016b

I want to plot and write text fro a loop in a live script. Unfortunately this puts all the text at the top and charts at the bottom. Does anyone know how to fix this?
for j = 1:3
figure
plot(1:10,j*ones(1,10))
drawnow
grid on
title(sprintf('This is plot %d',j))
fprintf('This is the text for plot %d\n',j)
drawnow
end
I have attached the live script for testing.

1 件のコメント

Nirav Sharda
Nirav Sharda 2017 年 1 月 24 日
This is a current limitation of the live editor. I work for MathWorks and have provided this feedback to the development team. Currently there are no known workarounds for this.

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

回答 (1 件)

Adam Wyatt
Adam Wyatt 2017 年 10 月 25 日
編集済み: Adam Wyatt 2019 年 4 月 18 日

1 投票

There is a slight workaround, which works in version 2017b (no need to manually create the axes, replace this and the title line with your own figure code, may or may not be necessary to clear the figure):
for n=1:3
fig = figure;
clf(fig);
axes;
title(n);
end
for n=10:12
fig = figure;
clf(fig);
axes;
title(n);
end
Note: Figures in the live editor work as follows: Matlab automatically creates a hidden figure window (i.e. a figure window with its visibility property set to false). By default, each plot is created in the current hidden figure window - overwriting or adding to existing plots according to the default properties of the command in question (e.g. plot will create a new axes, whereas line will draw a line in the current axes, unless hold is on). After all plotting commands are complete, Matlab will export the figure to a bitmap and then insert this figure into the live editor (which is just an HTML parser).
Understanding this process should help understand how to implement your requirements. For example, you can resize the figure window, thus changing the size of the image imported into the document.
Note that an "excessive" amount of figures (particularly large figures) will dramatically slow down the interface. There are pros and cons of live scripts and unfortunately Mathworks haven't quite got it to the stage that they are the optimum solution in the majority of scenarios.

5 件のコメント

Olaf Bousche
Olaf Bousche 2017 年 10 月 26 日
編集済み: Olaf Bousche 2017 年 10 月 26 日
I agree that the plots are more efficient. But that still leaves the problem of the text from fprintf being in one block and the plots in the next.
Not even this works
close all
for j = 1:3
plot_this(j)
print_this(j)
end
%%Dummy function
function plot_this(j)
figure
plot(1:10,j*ones(1,10))
ylim([0,5])
grid on
title(sprintf('This is plot %d',j))
end
function print_this(j)
fprintf('This is the text for plot %d\n',j)
end
Adam Wyatt
Adam Wyatt 2018 年 11 月 15 日
In R2018b, the text appears immediately after the figure. Cannot comment on earlier versions. You can always use the text command to insert text into the figure itself.
Note it works without the clf or axes commands:
for n=3:5
figure
plot(randn(10, n));
fprintf('Random data %d times', n);
end
Olaf Bousche
Olaf Bousche 2018 年 11 月 15 日
編集済み: Olaf Bousche 2018 年 11 月 15 日
Adam,
Thanks for reminding me. The live scripts have been significantly improved since they were introduced. In 2018b my original script shown at the top:
for j = 1:3
figure
plot(1:10,j*ones(1,10))
drawnow
grid on
title(sprintf('This is plot %d',j))
fprintf('This is the text for plot %d\n',j)
drawnow
end
works as it should! It took a few versions but now things are working as intended.
Adam Wyatt
Adam Wyatt 2023 年 3 月 3 日
Unfortunately changes in the way live scripts work means this solution no longer works (or at least no longer works reliably).
This is ridiculous that such a simple and important capability is missing (or at least not documented). @MathWorks Support Team - please fix this!
Olaf Bousche
Olaf Bousche 2023 年 3 月 3 日
Dear Adam:
This has been fixed a while ago. I thing it was fixed in R2019b and certainly R2022b has no problems with the orginal script. In fact the script below works flawless now.
for j = 1:3
figure
plot(1:10,j*ones(1,10))
grid on
title(sprintf('This is plot %d',j))
fprintf('This is the text for plot %d\n',j)
end

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

カテゴリ

ヘルプ センター および File ExchangeAxes Appearance についてさらに検索

製品

質問済み:

2017 年 1 月 19 日

コメント済み:

2023 年 3 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by