Borrar filtros
Borrar filtros

Assigning imported data to variable names (also imported)

8 visualizaciones (últimos 30 días)
chemeng100
chemeng100 el 29 de Mzo. de 2017
Comentada: chemeng100 el 29 de Mzo. de 2017
Hello everyone,
I have a problem to assign names to data from imported excel file.
The data is exported from the file:
filename='Input parameters.xlsx';
sheet='Sheet1';
xlRange='A1:B21';
[VarVal VarNam]=xlsread('Input parameters.xlsx',sheet,xlRange)
The assignment I use is as follows:
vals=VarVal'
vars = VarNam'
for i = vals
eval([vars{i} '= vals(i)'])
end
Anyone knows how to fix it? Basically the purpose is that I would like to insert parameters into excel and then run my model in matlab without interfering in command window.

Respuesta aceptada

Stephen23
Stephen23 el 29 de Mzo. de 2017
Editada: Stephen23 el 29 de Mzo. de 2017
If you want to write efficient code that is easy to debug, read my answer below. If you wish to use eval to assign values to variables (for some reason beginners often want to do this), then read these first:
A much better solution is to convert the data into a structure:
>> VarVal = [1,2,3];
>> VarNam = {'A','B','C'};
>> V = vertcat(VarNam,num2cell(VarVal));
>> S = struct(V{:});
>> S.A
ans = 1
>> S.C
ans = 3
If you have a newer MATLAB version, a table might be suitable too:
See, MATLAB is simple once you stop using awful eval.
  1 comentario
chemeng100
chemeng100 el 29 de Mzo. de 2017
That is what I was searching for.
Thank you very much for quick response.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by