Add lines to a struct

30 ビュー (過去 30 日間)
Bruce Victor
Bruce Victor 2021 年 12 月 27 日
コメント済み: Stephen23 2021 年 12 月 27 日
I have a struct where one of the columns is the PatientID that appears like this 'P00000001' and I want to add more patients with a diferent PacientID but with the same structer ('P00000001' - a string with P and 8 numbers) how do I do?
  2 件のコメント
Matt J
Matt J 2021 年 12 月 27 日
編集済み: Matt J 2021 年 12 月 27 日
structs do not have "columns". Are you sure you don't mean a table?
Stephen23
Stephen23 2021 年 12 月 27 日
"structs do not have "columns""
This structure has three columns:
S = struct('A',{1,2,3;4,5,6})
S = 2×3 struct array with fields:
A
size(S,2)
ans = 3

サインインしてコメントする。

回答 (1 件)

Adam Danz
Adam Danz 2021 年 12 月 27 日
編集済み: Adam Danz 2021 年 12 月 27 日
s = struct();
s.(sprintf('P%08.0f',1)) = 'Baetriz';
s.(sprintf('P%08.0f',2)) = 'Adam';
s.(sprintf('P%08.0f',3)) = 'Matlab'
s = struct with fields:
P00000001: 'Baetriz' P00000002: 'Adam' P00000003: 'Matlab'
Or if you're refering to tables,
T = table();
T.(sprintf('P%08.0f',1)) = 'Baetriz';
T.(sprintf('P%08.0f',2)) = 'Adam';
T.(sprintf('P%08.0f',3)) = 'Matlab'
T = 1×3 table
P00000001 P00000002 P00000003 _________ _________ _________ Baetriz Adam Matlab
  2 件のコメント
Steven Lord
Steven Lord 2021 年 12 月 27 日
If you're using a table you probably want each patient to be a row of data, with the IDs as the RowNames.
t = table("Baetriz", 'VariableNames', "Name", 'RowNames', "P00000001")
t = table
Name _________ P00000001 "Baetriz"
t{sprintf("P%08d", 2), 'Name'} = "Steve"
t = 2×1 table
Name _________ P00000001 "Baetriz" P00000002 "Steve"
n = t{'P00000001', 'Name'}
n = "Baetriz"
Adam Danz
Adam Danz 2021 年 12 月 27 日
Or if you want to add row names all at once,
names = ["Baetriz";"Steve";"Adam"]
names = 3×1 string array
"Baetriz" "Steve" "Adam"
rownames = compose('P%08d',1:numel(names))
rownames = 1×3 cell array
{'P00000001'} {'P00000002'} {'P00000003'}
T = table(names, 'RowNames', rownames)
T = 3×1 table
names _________ P00000001 "Baetriz" P00000002 "Steve" P00000003 "Adam"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by