How to create an structure fields from an cell array?

5 次查看(过去 30 天)
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

回答(2 个)

madhan ravi
madhan ravi 2018-12-14
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 2018-12-14
编辑:per isakson 2018-12-14
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]

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by