loop and save problem

1 visualización (últimos 30 días)
N
N el 14 de Ag. de 2014
Comentada: Michael Haderlein el 14 de Ag. de 2014
I have a problem with a loop I made and how I want to save it: My basic code without loop is below. I want to make this in a loop because I do not always know the amount of parts. Then I would like to save this in a structure called part. one, part.two, part.three, ...
part1 = [trigger2(endpoint(1):startpoint(2))];
part2 = [trigger2(endpoint(2):startpoint(3))];
part3 = [trigger2(endpoint(3):startpoint(4))];
part4 = [trigger2(endpoint(4):length(trigger2))];
end
The loop I have made so far is below. It seems to work but off course override the data each time (I made part 22 so check if it worked, the idea would be that it would get saved as part x). I don't know how to save it the way I want to, can anybody help? probably easy but I cant seem to figure it out.
for i = 1:length(startpoint)
if startpoint == length(startpoint)
part = [trigger2(endpoint(i):startpoint(i+1))];
else
part22 = [trigger2(endpoint(i):length(trigger2))];
end
end
  3 comentarios
Joakim Magnusson
Joakim Magnusson el 14 de Ag. de 2014
It would be easier to answer if you put up an example of how your data can look like before and after the loop.
Michael Haderlein
Michael Haderlein el 14 de Ag. de 2014
Did you try the two suggestions from Joakim and me? Did they do what you want? If not, please say what the outcome of our functions is and how it differs from what you expect.

Iniciar sesión para comentar.

Respuesta aceptada

N
N el 14 de Ag. de 2014
Actually a colleague found the answer for me, it was incredibly simple, just putting it in a cell instead of a structure. Thanks anyway for allt he help here, i appreciate it immensely !!!
  1 comentario
Michael Haderlein
Michael Haderlein el 14 de Ag. de 2014
That's what I have suggested in my answer...

Iniciar sesión para comentar.

Más respuestas (2)

Joakim Magnusson
Joakim Magnusson el 14 de Ag. de 2014
Not sure what you are trying to do, but maybe you mean like this?
for i = 1:length(startpoint)
if startpoint == length(startpoint)
part = [part trigger2(endpoint(i):startpoint(i+1))];
else
part22 = [part22 trigger2(endpoint(i):length(trigger2))];
end
end

Michael Haderlein
Michael Haderlein el 14 de Ag. de 2014
I don't know the context of your question, but I'm pretty sure that a structure is not the best solution for that. Also, the name "startpoint" and "endpoint" is a bit counter-intuitive in this context, but maybe it makes more sense in the context of your complete program. Anyway, if you want to save parts of this trigger2 vector and the parts might be of individual size, I'd use a cell:
trigger2=1:20;
startpoint=[1 4 9 17];
endpoint=[3 7 15 18];
part=cellfun(@(l,h) trigger2(l:h),...
num2cell(endpoint),...
num2cell([startpoint(2:end),length(trigger2)]),...
'uni',false);

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