0

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 });

2 Answers 2

1

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'
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks so much! I knew it would be something simple. Oh the joys of being a novice.
Thanks so much! I knew it would be something simple. Oh the joys of being a novice. While what you explain works perfectly at getting my the output out, there is one more think I would like to try and do. As explained at the end I would like to be able to end up with { Content }, { Content}, etc. So that when I insert curSet into my the function that needs it it creates those. Any suggestions? Would adding the relevant elements in the form of '{' work?
@Bastien Sorry, I don't get what you mean. Do you mean adding a final line? With what content exactly?
Sorry I probably wasn't super clear. If you look into my original post I am basically writing this loop to generate a series of lines which in the function I am using to load a series of data sets into my analysis software wants arrays (one for each set as we have created with that loop) If I was to manually write the sets one by one I would end up with something like that: doSomething(A, 'command',{... {'index 1 'load' path/file 'subject' 505 'condition' HiLabel}, {'index 2 'load' path/file 'subject' 505 'condition' LoLabel} etc });
No worries, I think I was creating the problem by not having made the output of the loop compatible with what was needed in the function. Your help was great anyway!
|
0

/edit Luis suggestion to create the cell array is better, removed my code.

A struct would be better to match the data structure you are creating. The code would look like:

subject = {'505','506'};
pathname_read  = 'a path';
nsubj = length(subject);
curIndex=0;
line=0;
curSet=[];
for s=1:nsubj
    Cond={'HiLabel','LowLabel'};
    ncond=length(Cond);
    for e=1:ncond;
        curIndex=curIndex+1;
        line=line+1;
        cr.index=curIndex;
        cr.load=[pathname_read subject{s} '_' Cond{e} '.set'];
        cr.subject=subject{s};
        cr.condition=Cond{e};
        curSet=[curSet cr];
    end
end 

1 Comment

Thanks for the super fast reply. I'm liking this idea and I've actually learned something I didn't know with this. Sadly I'm afraid I have no way of keeping the bits of strings that were in my original plan. Would that output get me a sort of list like it does in the implementation above? The result it returns is clearly a structure which I may struggle to insert in my function later on. Let me know, I'm keen to learn these things. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.