How to create an structure fields from an cell array?

9 views (last 30 days)
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

Answers (2)

madhan ravi
madhan ravi on 14 Dec 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 on 14 Dec 2018
Edited: per isakson on 14 Dec 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]

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!