- 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?
43 ビュー (過去 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!