How to create an structure fields from an cell array?

9 Ansichten (letzte 30 Tage)
Luis Garcia
Luis Garcia am 14 Dez. 2018
Bearbeitet: per isakson am 14 Dez. 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

Antworten (2)

madhan ravi
madhan ravi am 14 Dez. 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 am 14 Dez. 2018
Bearbeitet: per isakson am 14 Dez. 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]

Kategorien

Mehr zu Structures 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