How can I plot several graphs with slightly different name in a loop?

10 ビュー (過去 30 日間)
Robert Berghaus
Robert Berghaus 2017 年 10 月 17 日
コメント済み: Rik 2017 年 10 月 17 日
I want to plot many graphs in a loop. But every plot should get a description like h1, h2 and so on, that I can change the settings after plotting all the data. How does this work? I have a read a lot that I need an array, but what do I have to change at the fourth line (names(i))?
x = {'2';'3';'4'};
names = [strcat('h',x)]
for i=[2,3,4]
names(i)=plot(xx,yy);
end
set(h2,'Linewidth',1.5);
  1 件のコメント
Stephen23
Stephen23 2017 年 10 月 17 日
編集済み: Stephen23 2017 年 10 月 17 日
"But every plot should get a description like h1, h2 and so on, that I can change the settings after plotting all the data."
No, they definitely shouldn't have lots of names like that! That would be about the worst way to write MATLAB code: slow, buggy, complex, hard to debug, insecure. Read this to know why:
All you need is to use indexing, which is simple and efficient, and is introduced very clearly in MATLAB's Getting Started tutorials (and which are highly recommended for all beginners):

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

採用された回答

Rik
Rik 2017 年 10 月 17 日
You were looking for eval. Stephen Cobeldick is very pasionate about this topic (and with good reason), so I have taken the liberty of adapting some of his thoughts on this topic: Introspective programming ( eval and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem.
The code below should solve your problem.
x = [2,3,4];
names = sprintfc('h%d',x);
h=zeros(size(names));
for i=[2,3,4]
yy=rand(1,10);xx=1:10;
h(i)=plot(xx,yy);hold on
end
set(h(2),'Linewidth',1.5);
legend(h(x),names)
  3 件のコメント
Robert Berghaus
Robert Berghaus 2017 年 10 月 17 日
編集済み: Robert Berghaus 2017 年 10 月 17 日
Works good. Thanks, but How can I add some text behind my number? For example:
names = sprintfc('h%d',x,'text');
Rik
Rik 2017 年 10 月 17 日
sprintfc works in a similar way as sprinft, so that string is the formatspec. You can look at the documentation (type doc sprintf) to see the full range of options.

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

その他の回答 (3 件)

Stephen23
Stephen23 2017 年 10 月 17 日
編集済み: Stephen23 2017 年 10 月 17 日
How MATLAB works is different to what you are attempting: you simply allocate values into the array using indexing, rather than creating an array of names and somehow magically allocating to them.
All you need is to use indexing:
lhnd = zeros(1,4);
for k = 1:4
lhnd(k) = plot(xx,yy);
end
set(lhnd(2),'Linewidth',1.5); % use indexing!
  2 件のコメント
Rik
Rik 2017 年 10 月 17 日
With eval he would have been right ;)
Stephen23
Stephen23 2017 年 10 月 17 日
@Rik Wisselink: ugh.

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


goerk
goerk 2017 年 10 月 17 日
You can use the eval command. Simple example:
eval(['h=plot([1:10])'])
In your case something like
eval(['h' num2str(i) '= plot(xx,yy)'])
should do the job.
  1 件のコメント
Stephen23
Stephen23 2017 年 10 月 17 日
編集済み: Stephen23 2017 年 10 月 17 日
Or you could read the MATLAB documentation and learn how to write simpler, more reliable, and much more efficient MATLAB code.
Like my answer: simpler and more efficient than this code. Why? Because I did not use eval.

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


KSSV
KSSV 2017 年 10 月 17 日
for i = 1:3
figure(i) ;
h(i) = plot(rand(10,1));
end
set(h(1),'linewidth',10)
  1 件のコメント
Robert Berghaus
Robert Berghaus 2017 年 10 月 17 日
I know that it is bad style, but this one helps a lot. Thank you.

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by