필터 지우기
필터 지우기

How i create a structure from an excell that i import into matlab ?

조회 수: 7 (최근 30일)
Leonidas Tempelis
Leonidas Tempelis 2021년 4월 9일
댓글: Walter Roberson 2021년 4월 9일
I have an excell table with dimencions (9x8). The table has text, number and time data. I want to import the table in matlab and then create a structure with 8 fields. Each field will have the values of each row, so the dimencions would be 9x1.
I tried to import the excell table as 8 column vectors. Then i tried create a stuctrure with 8 fields where each field would correspond to each vector. The following code describes what i have done already.
% initialize an empty list of incomplete paths H
% then fulfill each field with the right column of the excell table
(line8) H = struct ( field1, value1, field2, value2, field3, value3, field4, value4, field5, value5, field6, value6, field7, value7, field8, value8);
H.value1 = 'ORIGIN';
H.value2 = 'DESTINATION';
H.value3 = 'EARLIESTPICKUPTIME';
H.value4 = 'LATESTDELIVERY';
H.value5 = 'TRANSPORTATION';
H.value6 = 'MODEOFTRANSPORT';
H.value7 = 'HRS';
H.value8 = 'KM';
My matlab file name is ''secondmatlab'' and the error message that appears when i try to run the code is the following:
secondmatlab
Undefined function or variable 'field1'.
Error in secondmatlab (line 8)
H = struct ( field1, value1, field2, value2, field3, value3, field4, value4, field5, value5, field6, value6, field7, value7, field8, value8);
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 4월 9일
H = struct ( field1, value1, field2, value2, field3, value3, field4, value4, field5, value5, field6, value6, field7, value7, field8, value8);
For that to work, field1 would need to be a variable that held a character vector or string scalar that is a field name. Perhaps you want
H = struct ( 'field1', value1, 'field2', value2, 'field3', value3, 'field4', value4, 'field5', value5, 'field6', value6, 'field7', value7, 'field8', value8);

댓글을 달려면 로그인하십시오.

답변 (1개)

Soumya Paliwal
Soumya Paliwal 2021년 4월 9일
When creating a struct like this:
s = struct(field1,value1)
the correct way to interpret it is like this:
s.field1 = value1
To add more fields, you should do the following:
s.field2 = value2
This will create a struct s with 2 fields, 'field1' and field2' with the values 'value1' and 'value2' respectively.
For your example, you can try doing the following:
% Read the excel file and store the data in a table
rawData = readtable('<excel_file_name>.xlsx');
% Do this for all the columns of the excel file
for i=1:width(rawData)
% myStruct.field<i> = rawData.<variableName>
myStruct.(['field' num2str(i)]) = rawData.(rawData.Properties.VariableNames{i});
end

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by