Error using eval Undefined function 'times' for input arguments of type 'table'.
이전 댓글 표시
I am working on crating 12 windroses for 2016 (one fore each month) but when I run the code I end up with:
Error using eval Undefined function 'times' for input arguments of type 'table'.
My code is:
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};
x{i} = eval([filename]);
[figure_handle, count, Speeds, Directions, Table] = WindRose(x(:,12), x(:,14), Options);
end
Suggestions?
댓글 수: 2
Stephen23
2018년 5월 24일
Duplicate earlier question:
Stephen23
2018년 5월 24일
"Suggestions?"
Yes: avoid using eval for trivial code like this. Accessing variable names dynamically using eval, assignin, etc. is slow, complex, buggy and hard to debug. Beginners think that they will write great code using eval, but actually they just force themselves into writing slow, complex, buggy code. The MATLAB documentation specifically advises against what you are trying to do: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
There are many good reasons to avoid using eval to dynamically access variable names, some of them are explained here:
Note that putting 1, 2, ... 12 into the variable names means that you have used pseudo indexing, which could trivially be turned into real indexing: thus you could easily write more efficient code.
Advice to use eval is teaching you how to write slow, complex code which is hard to debug. If you want to write simpler, more efficient code which is much easier to read, write, and debug, then put your data into one array and use indexing.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!