I'm pretty new to matlab but after hours of trying to find a way to solve my problem it seems I have to ask directly as nothing I found really helps directly
What I am trying to do is to build a cell array that contains various variables that I have to loop over. At the minute what I have manages to create this cell array pretty well but the loop overwrites the results. I've trying looking for ways to save the output and have managed but only with examples that did not involve a cell array :(
Here's the code (forgive me if it looks really bad):
subject = {'505','506'}
pathname_read = 'a path';
nsubj = length(subject);
curIndex=0;
for s=1:nsubj
Cond={'HiLabel','LowLabel'};
ncond=length(Cond);
for e=1:ncond;
curIndex=curIndex+1
line=line+1
curCond=Cond{e};
curFile=[pathname_read subject{s} '_' Cond{e} '.set'];
curSubject=subject{s};
curSet={'index' curIndex 'load' curFile 'subject' curSubject 'condition' curCond};
end
end
the curSet is the cell array that gets built up. I've seen ways to extract from a loop with something like curSet(e) but here it doesn't work.
Ultimately the result I want is something like that:
curSet=
{'index 1 'load' path/file 'subject' 505 'condition' HiLabel};
{'index 2 'load' path/file 'subject' 505 'condition' LoLabel};
{'index 3 'load' path/file 'subject' 506 'condition' HiLabel};
{'index 4 'load' path/file 'subject' 506 'condition' LoLabel};
I'd also want to find a way to get the ; after each line. I suppose it could, once all collected, be a kind of string since it will get "pasted into a function that looks like this
doSomething(A, 'command',{ My generated curSet });
Change your line
curSet = {'index' curIndex 'load' curFile 'subject' curSubject 'condition' curCond};
to
curSet(curIndex,:) = {'index' curIndex 'load' curFile 'subject' curSubject 'condition' curCond};
That way you add a row at each iteration, instead of overwriting. The final result is
>> curSet
curSet =
'index' [1] 'load' [1x21 char] 'subject' '505' 'condition' 'HiLabel'
'index' [2] 'load' [1x22 char] 'subject' '505' 'condition' 'LowLabel'
'index' [3] 'load' [1x21 char] 'subject' '506' 'condition' 'HiLabel'
'index' [4] 'load' [1x22 char] 'subject' '506' 'condition' 'LowLabel'