フィルターのクリア

From char to double for plotting

4 ビュー (過去 30 日間)
Nicolò Monaco
Nicolò Monaco 2022 年 6 月 18 日
編集済み: Bjorn Gustavsson 2022 年 6 月 18 日
Hello,
I have n=12 "double" data set called I_n. I want a subplot (histogram in this case), for each data set. With the following code the function histogram doesn't recognize the "char" value. How can I better implement this?
figure()
for n=1:12
chartname = strcat('I_',num2str(n));
subplot(3,4,n)
histogram(chartname)
hold on
end

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2022 年 6 月 18 日
編集済み: Bjorn Gustavsson 2022 年 6 月 18 日
First you should store the data-sets in some kind of array. For the most tolerant storage (that doesn't care about different dimenstions or data-type) use cell-arrays. Then you can do somethinh like this:
figure
for n = 1:12
curr_I = I_all_in_cell{n};
subplot(3,4,n)
histogram(curr_I)
hold on
end
Do not use numbered variables for this type of work, use the different arrays that matlab makes it possible to work with. Just build the array instantly at the point where you load the data. Also look after the numerber of times this type of question have been asked for a more in-depth explanations.
HTH

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 6 月 18 日
Do this klunky way:
subplot(3,4,1)
histogram(I_1)
subplot(3,4,2)
histogram(I_2)
subplot(3,4,3)
histogram(I_3)
subplot(3,4,4)
histogram(I_4)
subplot(3,4,5)
histogram(I_5)
subplot(3,4,6)
histogram(I_6)
subplot(3,4,7)
histogram(I_7)
subplot(3,4,8)
histogram(I_8)
subplot(3,4,9)
histogram(I_9)
subplot(3,4,10)
histogram(I_10)
subplot(3,4,11)
histogram(I_11)
subplot(3,4,12)
histogram(I_12)
Now you know why it's not good to have so many uniquely-named variables. You should put them all into a matrix, like @Bjorn Gustavsson suggested, if you can. Then you could use a simple, compact for loop like you wanted.
See the FAQ:

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by