MATLAB: Hi, i have cell that consisting string value.i want to use every cell element as variable name. for example filename{1}= [1 2 3] then i have EX1=[1 2 3] in work space. i wrote this code but it doesn’t work.

cell arraysvariable namevariables

filename={'EX1','EX2','FX1','FX2','PR1','PR2',...
'RU1','RU2','SP1','SP2','UL1','UL2'};
for i=1:12
genvarname(filename{i})) =importdata([filename{i},'.mat']);
end

Best Answer

As an alternative to magically creating variable names (which is not recommended), you can very simply define a structure and dynamically create the fieldnames:
C = {'EX1','EX2','FX1','FX2','PR1','PR2','RU1','RU2','SP1','SP2','UL1','UL2'};
S = struct();
for k = 1:numel(C)
S.(C{k}) = importdata(sprintf('%s.mat',C{k}));
end
and then accessing your data is also very simple and efficient:
S.EX1
S.EX2
S.FX1
... etc