Error using eval Undefined function 'times' for input arguments of type 'table'.
5 次查看(过去 30 天)
显示 更早的评论
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
"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.
采纳的回答
Jan
2018-5-23
Please read and consider Tutorial: Why to avoid eval. Hiding indices in names of variables is a really really bad idea.
But in your code, there must be a further problem. After
filename = sprintf('%s_%d', 'Month', i);
you get something like the char vector 'Month_1' as filename. Then in x{i}=eval(filename) (no need for square brackets here), there cannot be an error message concerning "times" and "tables". The only possible explanation is, that you do not show us the code, which produces the error. Maybe "Month_1" if an M-file, which contains the failing code?
This would be clear, if you post the complete error message. But you can check the problem by your own also. Use the debugger by typing this in the command window:
dbstop if error
Now run the code again, an when it stops, check the the variables used in the current line.
2 个评论
Stephen23
2018-5-24
"The only possible explanation is, that you do not show us the code, which produces the error..."
And is a good example of how writing obfuscated code using eval makes debugging harder. None of the debugging tools work with eval, so beginners who decide to use it force themselves into searching for a black cat in a dark room with the light turned off.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!