Borrar filtros
Borrar filtros

How to create an structure fields from an cell array?

3 visualizaciones (últimos 30 días)
Luis Garcia
Luis Garcia el 14 de Dic. de 2018
Editada: per isakson el 14 de Dic. de 2018
I have the following code:
data1 = xlsread('data1.xlsx');
namesoftags = {'timeaxis','cputime','flux','volts'};
for i =1:4
S = cell2struct(data1(:,i),namesoftags(i));
end
data1 has the following data:
It's a 5x4.
1 5 298 53
2 9 284 35
3 0 58 2329
4 17 892 67
45 183 45 29
But its gives me this error:
Error using cell2struct
Unknown command option.
Error in structuredemo (line 4)
S = cell2struct(data1(:,i),namesoftags(i));
Thankyou

Respuestas (2)

madhan ravi
madhan ravi el 14 de Dic. de 2018
Use readtable() requires 2016b or later to read the excel file and then give table the variable names and then simply use table2struct() to convert it into structure with fieldnames(which will be the variable names) , I suspect loop is superfluos in your case.

per isakson
per isakson el 14 de Dic. de 2018
Editada: per isakson el 14 de Dic. de 2018
The old way
%%
namesoftags = {'timeaxis','cputime','flux','volts'};
data1 = [ 1,5,298,53
2,9,284,35
3,0,58,2329
4,17,892,67
45,183,45,29 ];
%%
S = cell2struct( num2cell(data1,1), namesoftags, 2 );
Display result
>> S
S =
struct with fields:
timeaxis: [5×1 double]
cputime: [5×1 double]
flux: [5×1 double]
volts: [5×1 double]

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by