MATLAB Answers

How do I rename each individual item in a column in one function?

1 view (last 30 days)
Dayne Chart
Dayne Chart on 24 Jun 2021
Answered: Adam Danz on 24 Jun 2021
I have a structure which has 15 fields with various columns. The first column is titled label and has the 15 conditions in it. I manually set these as before they were all called 'Undefined' and realised I could change each one with the function D.trials(1).label = "Single_L_M' and then repeat all the way till D.trials(15).label = "ISI200_R_M'. Is there a way in one function to rename the lables rather than 15 lines which I have currently:
D.trials(1).label = 'Single_L_M';
D.trials(2).label = 'Single_L_I';
D.trials(3).label = 'Single_R_I';
D.trials(4).label = 'Single_R_M';
D.trials(5).label = 'Double_L_M';
D.trials(6).label = 'Double_R_I';
D.trials(7).label = 'Double_R_M';
D.trials(8).label = 'ISI30_L_M';
D.trials(9).label = 'ISI30_L_I';
D.trials(10).label = 'ISI30_R_I';
D.trials(11).label = 'ISI30_R_M';
D.trials(12).label = 'ISI200_L_M';
D.trials(13).label = 'ISI200_L_I';
D.trials(14).label = 'ISI200_R_I';
D.trials(15).label = 'ISI200_R_M';

Answers (1)

Adam Danz
Adam Danz on 24 Jun 2021
Create a list of the new names within a cell array and then use deal to distribute the list to the label fields.
Demo:
D.trials(1).label = 'Single_L_M';
D.trials(2).label = 'Single_L_I';
D.trials(3).label = 'Single_R_I';
newNames = {'ABC', 'DEF', 'GHI'};
[D.trials.label] = deal(newNames{:});
D.trials.label
ans = 'ABC'
ans = 'DEF'
ans = 'GHI'

Community Treasure Hunt

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

Start Hunting!

Translated by