필터 지우기
필터 지우기

loop and save problem

조회 수: 3 (최근 30일)
N
N 2014년 8월 14일
댓글: Michael Haderlein 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 !!!
  댓글 수: 1
Michael Haderlein
Michael Haderlein 2014년 8월 14일
That's what I have suggested in my answer...

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by