loop and save problem

4 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by