필터 지우기
필터 지우기

How to create an structure fields from an cell array?

조회 수: 3 (최근 30일)
Luis Garcia
Luis Garcia 2018년 12월 14일
편집: per isakson 2018년 12월 14일
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