Filter löschen
Filter löschen

how can I add a table to a structure?

48 Ansichten (letzte 30 Tage)
Michael O'Brien
Michael O'Brien am 13 Mär. 2023
Verschoben: Stephen23 am 14 Mai 2023
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 Kommentar
Stephen23
Stephen23 am 13 Mär. 2023
"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:
  • 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?
It looks as if you mixed up the rows and columns in the second sentence.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Mär. 2023
Verschoben: Stephen23 am 13 Mär. 2023
table2struct perhaps ?
  1 Kommentar
Michael O'Brien
Michael O'Brien am 13 Mär. 2023
Verschoben: Stephen23 am 14 Mai 2023
I'm in the UK so at different timezones to other community members on here that commented and have given answers. @Walter Roberson, my guy, smashed it. If I could accept a comment as an answer then I would. I was tryung so many things with for loops and cells and sprintf (it was 3am) and couldn't do it. I knew there had to be a simple way; table2struct literally was exactly what I was looking for. THANK YOU and thank you to the other people that took time out to help, it's super appreciated.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Adam Drake
Adam Drake am 13 Mär. 2023
Bearbeitet: Adam Drake am 13 Mär. 2023
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 = 1×10 struct array with fields:
field1 field2 field3 field4 field5 field6 field7 field8
s.field1
ans = '1'
ans = '2'
ans = '3'
ans = '4'
ans = '5'
ans = '6'
ans = '7'
ans = '8'
ans = '9'
ans = '10'
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)
ans = struct with fields:
field1: '1' field2: [0 0 0 0 0 0 0 0 0 0] field3: [1 1 1 1 1 1 1 1 1 1] field4: 'fourth' field5: 'fifth' field6: 'sixth' field7: 'seventh' field8: 'eighth' field9: [1×1 struct]
s(1).field9
ans = struct with fields:
Age: 38 Height: 71 Weight: 176 Systolic: 124 Diastolic: 93
Structure "s" now contains a ninth field with table variable names and values.
  1 Kommentar
Michael O'Brien
Michael O'Brien am 13 Mär. 2023
Thank you so much. I got it working with @Walter Roberson suggestion in the first comment.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by