Filter löschen
Filter löschen

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.

1 Ansicht (letzte 30 Tage)
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
  4 Kommentare
Guillaume
Guillaume am 22 Feb. 2018
So, you're having difficulty generating dynamic variable names. Even if we told you how to do it, you would have the exact same difficulties every time you want to use these variable names. The only good answer to your question is don't do that.
I'm sure Stephen will answer your question with his very detailed explanation of why it's a bad idea. Please follow that advice.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Feb. 2018
Bearbeitet: Stephen23 am 22 Feb. 2018
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

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by