Borrar filtros
Borrar filtros

Error using eval Undefined function 'times' for input arguments of type 'table'.

3 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 24 de Mayo de 2018
"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.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 23 de Mayo de 2018
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 comentarios
Stephen23
Stephen23 el 24 de Mayo de 2018
"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.
Jan
Jan el 24 de Mayo de 2018
...and it is a cat of Schrödinger not knowing in advance what to do next.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by