フィルターのクリア

How to store a cell array as a table cell?

68 ビュー (過去 30 日間)
azarang asadi
azarang asadi 2022 年 10 月 19 日
回答済み: Eshan Patel 2022 年 11 月 3 日
Hi all,
I have a structure with 5 fileds:
struct.field1 --> 1*8 cell
struct.field2 --> singe value (double)
struct.field3 --> 1*8 cell
struct.field4 --> 1*8 matrix (double)
struct.field5 --> 1*8 cell
and I also have a string let's call it RowName.
Now what I'm trying to do is to create a table with 6 variables and do a for loop which fills each row at each iteration. in each iteration, we need to do some calculations to get the struct and then assign its fileds to a row of the table. Here's how I did it:
T = table('Size',[0,6], 'VariableTypes',{'string','cell', 'double', 'cell', 'cell', 'cell'}, 'VariableNames',{'name','Var1', 'Var2', 'Var3', 'Var4', 'Var5'});
for i = 1:endLoop
T(end+1,:) = {RowName, struct.field1, struct.field2, struct.field3, {struct.field4}, struct.field5};
end
this doesn't work so I wonder how I can do it.
Also one more question:
struct.field4 is originally a 1*8 vector (double). Can I store it as a vector not a cell in my table? cause the code above stores it as a cell.
Thank you.
  1 件のコメント
dpb
dpb 2022 年 10 月 19 日
"struct.field4 is originally a 1*8 vector (double). Can I store it as a vector not a cell in my table?"
No. A given row for in a table for a variable is only one storage location -- the scalar cell variable contaiming the vector is that one thing that can go there.
This doesn't appear to be a good fit for the table class...

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

回答 (1 件)

Eshan Patel
Eshan Patel 2022 年 11 月 3 日
Hey Azarang,
I understand that you would like to save the values from the struct into a table. Since the struct has 5 fields (+ "rowName"), I am assuming you would like to have 6 columns (variables), and 8 rows to represent the 8 values in your fields.
To achieve this, you can use the following snippet of code:
T = table('Size', [8, 6], 'VariableTypes', ["string", "double", "double", "double", "double", "string"], ...
'VariableNames',{'name','Var1', 'Var2', 'Var3', 'Var4', 'Var5'});
for i = 1:8
T(i, :) = {rowName{i}, struct.field1{i}, struct.field2, ...
struct.field3{i}, struct.field4(i), struct.field5{i}};
end
assuming "rowName" is also a vector of strings.
To answer your second question - whether it would be possible to store "struct.field4" as a vector - no, it would not be possible to store your original vector field as a vector. However, you can obtain the vector back from the table using the following line of code:
a = [T{:, "Var4"}].';
% or
b = table2array(T(:, "Var4")).';

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by