Plot data in loop with num2str

9 ビュー (過去 30 日間)
yoni verhaegen
yoni verhaegen 2018 年 7 月 23 日
コメント済み: Stephen23 2018 年 7 月 24 日
Hi all,
I want to plot a dynamic figure by plotting the sixth column of the datasets Meteo1752 to Meteo1966:
balyr=1752;
endyr=1966;
for k = balyr:endyr
balanceyear(k-balyr+1,:)=k;
plot(['Meteo' num2str(k) '(:,6)']);
end
However, the error says
Error using plot
Invalid first data argument.
What am I doing wrong?
Thanks
  2 件のコメント
Adam
Adam 2018 年 7 月 23 日
編集済み: Adam 2018 年 7 月 23 日
doc plot
tells you what the valid data types are for the inputs. plot expects numeric data arguments (or an axes handle as first argument). You are giving it a string.
You'll have to give your actual data to the plot instruction, and probably you will need to use
doc hold
too.
Stephen23
Stephen23 2018 年 7 月 24 日
@yoni verhaegen: what you are trying to do is not recommended. Read this to know why:
It is easy to avoid this situation: usually some indexing is the key.

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

回答 (2 件)

Dimitris Kalogiros
Dimitris Kalogiros 2018 年 7 月 23 日
If you want to display something on your workspace, use disp() instead of plot()
balyr=1752;
endyr=1966;
for k = balyr:endyr
balanceyear(k-balyr+1,:)=k;
%plot(['Meteo' num2str(k) '(:,6)']);
disp(['Meteo' num2str(k) '(:,6)']);
end

dpb
dpb 2018 年 7 月 23 日
編集済み: dpb 2018 年 7 月 23 日
Several things...the specific error is trying to plot() a string; ML will not do character substitution like that.
The more fundamental issue is in having created the multiple variables with sequential names; as you've just discovered, now you have a problem in addressing them.
See the FAQ that talks about how to avoid the problem; we don't know how these were created so the best way to choose isn't clear.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by