Index exceeded matrix dimensions

1 回表示 (過去 30 日間)
Cameron Power
Cameron Power 2018 年 5 月 23 日
編集済み: Majid Farzaneh 2018 年 5 月 24 日
I am trying to create 12 windroses for each month of a year using data in the workspace (variable but around 720 by 24 tables) and each time I try to run the code below I get the index exceeded error message. I have managed to get the windrose figure for one month but wanted to automate the process with a for loop, suggestions.
nFiles = 12;
for i = 1:nFiles
filename = sprintf('%s_%d', 'Month', i);
Options = {'anglenorth', 0, 'angleeast', 90, 'labels', {'North (0)', 'South (180)', 'East (90)', 'West (270)'}, 'Min_Radius', .05, 'nFreq', 8, 'FreqRound', 3, 'freqlabelangle', 45, 'cMap','invparula' 'vWinds', [0 5 5 10 10 15 15 20 20 25 25 30 30 35 35 40], 'TitleString',{'January';''},'LabLegend','Wind Speed in km/h', 'TitleFontWeight', 'bold', 'LegendType', 2};
[figure_handle, count, Speeds, Directions, Table] = WindRose(filename(:,12) .* 10, filename(:,14), Options);
end
  5 件のコメント
Cameron Power
Cameron Power 2018 年 5 月 23 日
I have seen as much but how would I convert (not literal, just keep the same result) this way using eval to a more efficient method?
nFiles = 12;
for i = 1:nFiles
filename = sprintf('%s_%d', 'Month', i);
Options = {'anglenorth', 0, 'angleeast', 90, 'labels', {'North (0)', 'South (180)', 'East (90)', 'West (270)'}, 'Min_Radius', .05, 'nFreq', 8, 'FreqRound', 3, 'freqlabelangle', 45, 'cMap','invparula' 'vWinds', [0 5 5 10 10 15 15 20 20 25 25 30 30 35 35 40], 'TitleString',{'January';''},'LabLegend','Wind Speed in km/h', 'TitleFontWeight', 'bold', 'LegendType', 2};
[figure_handle, count, Speeds, Directions, Table] = WindRose(eval([filename,'{:,12} .* 10']), eval([filename,'{:,14}']), Options);
end
Stephen23
Stephen23 2018 年 5 月 23 日
@Cameron Power: the problem is not eval in itself, it is how beginners access variable names dynamically. The same problems occur regardless of what function or method is used to access the variable names dynamically. So, the best advice you will get is to avoid this situation entirely, which is trivial to do using one variable and indexing, exactly like the MATLAB documentation and all MATLAB experts recommend.
Read the links that I gave you, you can learn why this is a bad practice, and how simple the (much better) alternatives are.

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

採用された回答

Majid Farzaneh
Majid Farzaneh 2018 年 5 月 23 日
編集済み: Majid Farzaneh 2018 年 5 月 24 日
Hi,
[filename = sprintf('%s%d', 'Month', i);]_ makes a string like this: Month_1
Then you have used filename(:,12) and filename(:,14) . It means you want 12th or 14th character in the string that it's not exist. for example Month_1 has 7 characters and filename length is 7. So for 12 and 14 you have the error "Index exceeded matrix dimensions".
  5 件のコメント
Cameron Power
Cameron Power 2018 年 5 月 23 日
I figured it out using eval, I changed to curly brackets and it worked normally, thank you.
Walter Roberson
Walter Roberson 2018 年 5 月 23 日

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by