If statement with table

5 次查看(过去 30 天)
elisbe
elisbe 2016-7-24
评论: elisbe 2016-7-24
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!

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-7-24
编辑:Azzi Abdelmalek 2016-7-24
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'}

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by