If statement with table

5 views (last 30 days)
elisbe
elisbe on 24 Jul 2016
Commented: elisbe on 24 Jul 2016
I am a developmental psychologist and use Matlab for data analyses. I am not proficient at all in writing scripts from scratch, so any help is much appreciated. I have 2 columns in a table. I want to modify the contents of the 1st column depending on the 2nd column.
For example,
C = {'bgin' 'NAN'; 'resp' '';'TRSP' '1'; 'TRSP' '0'};
If row1 = 'TRSP' and row2 = '1' --> row1 should be changed to 'cor'.
If row1 = 'TRSP' and row2 = '0' --> row1 should be changed to 'inc'.
Else row1 = row1 (i.e. no change)
In other words, referring to the example above, I need C to look as follows at the end:
C = {'bgin' 'NAN'; 'resp' '';'cor' '1'; 'inc' '0'};
And I need these logical statements to apply to all the rows (~1000) in the table.
I'd very much like to do this in Matlab as I have more than 1500 files to change and at least know how to loop through files in Matlab.
Thanks in advance!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 24 Jul 2016
Edited: Azzi Abdelmalek on 24 Jul 2016
C is not a table it's a cell array
C = {'bgin' 'NAN'; 'resp' '';'TRSP' '1'; 'TRSP' '0'}
idx1=ismember(C(:,1),'TRSP') & ismember(C(:,2),'1')
idx2=ismember(C(:,1),'TRSP') & ismember(C(:,2),'0')
C(idx1,1)={'cor'}
C(idx2,1)={'inc'}
  1 Comment
elisbe
elisbe on 24 Jul 2016
Excellent! Works great. Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!