Create a table inside a structure

61 ビュー (過去 30 日間)
Camilo Jurado Paez
Camilo Jurado Paez 2021 年 3 月 9 日
コメント済み: Camilo Jurado Paez 2021 年 3 月 12 日
I want to create a table inside a structure but I haven't been able to do so with my code
f = struct ('SectionName',{},'SectionID',{},'Measurements',...
table ('MeasName',{},'DateTimeStart',{},'TimeDuration',{},...
'Vehicle',{},'Spped',{},'Direction',{},'Lane',{},'VehPosition',{},...
'Segment',{},'DataLabels',cell ({}),'DataWeighting',cell ({}),...
'DataUnits',cell ({}),'TimeRaw_sec',[],'DataRaw',[]));
Thank you so much if anyone might help

採用された回答

Steven Lord
Steven Lord 2021 年 3 月 9 日
This is not the correct way to create a table array with the specified variable names. The struct function accepts field names and field values as name-value pairs but table does not accept alternating variable names and variable values.
%{
T = table ('MeasName',{},'DateTimeStart',{},'TimeDuration',{},...
'Vehicle',{},'Spped',{},'Direction',{},'Lane',{},'VehPosition',{},...
'Segment',{},'DataLabels',cell ({}),'DataWeighting',cell ({}),...
'DataUnits',cell ({}),'TimeRaw_sec',[],'DataRaw',[]);
%}
You want to specify the 'VariableNames' option to construct this table, and I would additionally specify the 'Size' and 'VariableTypes' options as well. I'm only going to create three variables, but you could create more.
T = table('Size', [2 3], ...
'VariableTypes', {'cell', 'string', 'double'}, ...
'VariableNames', ["C", "S", "D"])
T = 2x3 table
C S D ____________ _________ _ {0×0 double} <missing> 0 {0×0 double} <missing> 0
T{2, "S"} = "abracadabra"
T = 2x3 table
C S D ____________ _____________ _ {0×0 double} <missing> 0 {0×0 double} "abracadabra" 0
  1 件のコメント
Camilo Jurado Paez
Camilo Jurado Paez 2021 年 3 月 12 日
Thank you so much! was really helpful

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

その他の回答 (1 件)

Jan
Jan 2021 年 3 月 9 日
編集済み: Jan 2021 年 3 月 9 日
Remember that struct('a', {}) creates an empty struct array. Then inserting a table should fail.
What is the purpose ofg cell({})? This is an empty cell as {} also.
The problem of your code is the error message:
Error using table:
Invalid parameter name: MeasName
This code creates the same message:
table('MeasName', {})
Based on the code, which does not run, I cannot guess, what you want to achieve. The problem is, that you cannot create a table with this command. It does not matter, that you try to do this inside a struct.
Explain, what you want to get as result.
  1 件のコメント
Camilo Jurado Paez
Camilo Jurado Paez 2021 年 3 月 12 日
Thank you! I figued it out and now it works!

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

カテゴリ

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