If statement with table

5 vues (au cours des 30 derniers jours)
elisbe
elisbe le 24 Juil 2016
Commenté : elisbe le 24 Juil 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!
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 24 Juil 2016
row 1 or column 1?

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 24 Juil 2016
Modifié(e) : Azzi Abdelmalek le 24 Juil 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 commentaire
elisbe
elisbe le 24 Juil 2016
Excellent! Works great. Thanks.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by