Rename multiple fields in a MATLAB struct -
i need rename bunch of fields in construction changing prefix on it.
e.g.
mystruct.your_firstfield mystruct.your_secondfield mystruct.your_thirdfield mystruct.your_forthfield %etc....
to
mystruct.my_firstfield mystruct.my_secondfield mystruct.my_thirdfield mystruct.my_forthfield %etc...
without typing each 1 out...since there many , may grow.
thanks!
you can dynamically generating field names output struct.
class="lang-m prettyprint-override">% illustration input mystruct = struct('your_firstfield', 1, 'your_secondfield', 2, 'your_thirdfield', 3 ); % cell array of mystruct's field names fields = fieldnames(mystruct); % create empty struct temp = struct; % loop through , assign each field of new struct after doing string % replacement. may need more complicated (regexp) string replacement % if field names not simple prefixes ii = 1 : length(fields) temp.(strrep(fields{ii}, 'your', 'my')) = mystruct.(fields{ii}); end % replace original struct mystruct = temp;
matlab struct rename field batch-rename
No comments:
Post a Comment