matlab - Remove first 2 letters from workspace variables -


let's have .mat file , know each of variables in has xy in front of (e.g. xyjanuary, xyfebruary, xymarch , on...) , want remove xy.

i have looked @ this , tried copy adds xy variable (xyxyjanuary, xyxyfebruay,...) want (january, februay,...).

x= load('file.mat');                             % load structure x names=fieldnames(x);                             % names of variables   iname=1:length(names)                      % start loop      x.(['xy', names{iname}]) = x.(names{iname}); % problem     x = rmfield(x, names{iname});   end save ('newfile.mat', '-struct', 'x');            %save 

x= load('file.mat');                              % load structure x names=fieldnames(x);                              % names of variables   iname=1:length(names)                       % start loop      x.([names{iname}(3:end)]) = x.(names{iname}); % no more problem     x = rmfield(x, names{iname});   end save ('newfile.mat', '-struct', 'x');             % save 

you added 'xy' lhs of line, makes add final solution. did chop off first 2 entries, keep rest, hence (3:end). works on test case created.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -