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!