loop and save problem
Show older comments
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 Comments
N
on 14 Aug 2014
Joakim Magnusson
on 14 Aug 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
on 14 Aug 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.
Accepted Answer
More Answers (2)
Joakim Magnusson
on 14 Aug 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
on 14 Aug 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);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!