for loop - splitting data

6 visualizaciones (últimos 30 días)
Theodore Bowen
Theodore Bowen el 13 de Abr. de 2017
Editada: Stephen23 el 19 de Jun. de 2019
I shall try to explain clearly, again.
I have a cell array:
Fish2 = Data{1,10,6};
I want to create a loop that will form the following groups:
Fish2_1 = Data{1,3,1};
Fish2_2 = Data{1,3,2};
I started with something like this:
C = 1:size(Fish2,3);
for i = 1:c
Fish2_i = Data(1,3,i);
end
This is obviously not operating the way i would like it to. I was hoping i would get 6 outputs, following the same pattern as the first coded sequence above.
I hope this is clear enough.
Thank you in advance.

Respuesta aceptada

ES
ES el 13 de Abr. de 2017
you have to do an eval.
eval(['Fish2_', num2str(i), ' = Data(1,3,i);'])
instead of
Fish2_i = Data(1,3,i);
  2 comentarios
Theodore Bowen
Theodore Bowen el 13 de Abr. de 2017
This is why i love matlab. Thank you very much.
Stephen23
Stephen23 el 14 de Abr. de 2017
Editada: Stephen23 el 14 de Abr. de 2017
DO NOT USE THIS CODE
This is not recommend by The Mathworks (who actually write MATLAB), nor by any experts on this forum. This is slow, buggy, and inefficient code that is loved by beginners, and by those who do not understand how to use MATLAB efficiently.
If any reader is actually interested in learning how to use MATLAB efficiently then read this documentation:
And also this thread (and all of its links):
@Theodore Bowen: please actually take the time to read the MATLAB documentation on this topic, and all of the links that I gave. Once you have learned why using eval totally destroys all JIT acceleration, all code checking tools, etc., then you can decide if you really want to use bad coding methods:
PS: you should actually be using indexing, exactly like I showed you in my answer to your earlier question:
Fish{k} = Data(1,3,i);
will be significantly faster, simpler, neater, easier to read, much less buggy, and easier to debug than any method using eval.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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