1

我需要通过基本上更改其前缀来重命名结构中的一堆字段。

例如

MyStruct.your_firstfield
MyStruct.your_secondfield
MyStruct.your_thirdfield
MyStruct.your_forthfield
%etc....

MyStruct.my_firstfield
MyStruct.my_secondfield
MyStruct.my_thirdfield
MyStruct.my_forthfield
%etc...

无需逐个输入...因为有很多并且可能会增长。

谢谢!

4

1 回答 1

3

您可以通过为输出结构动态生成字段名称来做到这一点。

% Example input
MyStruct = struct('your_firstfield', 1, 'your_secondfield', 2, 'your_thirdfield', 3 );

% Get a cell array of MyStruct's field names
fields = fieldnames(MyStruct);

% Create an empty struct
temp = struct;

% Loop through and assign each field of new struct after doing string 
% replacement. You may need more complicated (regexp) string replacement
% if field names are not simple prefixes
for ii = 1 : length(fields) 
  temp.(strrep(fields{ii}, 'your', 'my')) = MyStruct.(fields{ii}); 
end

% Replace original struct
MyStruct = temp;
于 2013-02-11T20:44:01.937 回答