- variable names are the column headers of a table (they are not the row names), so this would mean each 9th field would consist of a table with one row and 11 columns/variables. This perfectly matches the table size that you give.
- you stated that the table only has 10 rows, so where does the extra row come from?
how can I add a table to a structure?
조회 수: 31 (최근 30일)
이전 댓글 표시
I have a 1x10 struct with 8 fields.
I also have a 10x11 table imported from a workbook.
How can I add the row data from the table as a 9th field?
I want to keep the column headers of the table and use them as the variable names for the 9th field in the structure.
As in the the 9th field of the structure will have 11 rows and the rows will be named after the 11 column headers from the table.
I believe it should be simple but I have been going round in circles.
Thanks in advance,
댓글 수: 1
Stephen23
2023년 3월 13일
"I want to keep the column headers of the table and use them as the variable names for the 9th field in the structure."
"As in the the 9th field of the structure will have 11 rows and the rows will be named after the 11 column headers from the table."
These two statements contradict each other:
It looks as if you mixed up the rows and columns in the second sentence.
채택된 답변
추가 답변 (1개)
Adam Drake
2023년 3월 13일
편집: Adam Drake
2023년 3월 13일
Had fun with this one. Let me know if you get it to work.
clc, clear variables
f1 = 'field1'; value1 = {'1','2','3','4','5','6','7','8','9','10'};
f2 = 'field2'; value2 = zeros(1,10);
f3 = 'field3'; value3 = ones(1,10);
f4 = 'field4'; value4 = 'fourth';
f5 = 'field5'; value5 = 'fifth';
f6 = 'field6'; value6 = 'sixth';
f7 = 'field7'; value7 = 'seventh';
f8 = 'field8'; value8 = 'eighth';
s = struct( f1,value1,...
f2,value2,...
f3,value3,...
f4,value4,...
f5,value5,...
f6,value6,...
f7,value7,...
f8,value8);
s
s.field1
load patients
T = table(Age(1:10),Height(1:10),Weight(1:10),Systolic(1:10),Diastolic(1:10));
T.Properties.VariableNames = {'Age','Height','Weight','Systolic','Diastolic'};
t = table2struct(T);
f9 = 'field9';
for i = 1:length(t)
s = setfield(s,{i},f9,t(i));
end
s(1)
s(1).field9
Structure "s" now contains a ninth field with table variable names and values.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!