loop and save problem

1 次查看(过去 30 天)
N
N 2014-8-14
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 个评论
Joakim Magnusson
Joakim Magnusson 2014-8-14
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 2014-8-14
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.

请先登录,再进行评论。

采纳的回答

N
N 2014-8-14
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 !!!

更多回答(2 个)

Joakim Magnusson
Joakim Magnusson 2014-8-14
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 2014-8-14
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);

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by