When creating a table, how do I specify the dimensions of a particular variable?

117 ビュー (過去 30 日間)
Rafael Cordero
Rafael Cordero 2021 年 7 月 11 日
回答済み: Steven Lord 2021 年 7 月 11 日
Hello all,
Lets say Im defining an empty table as so:
my_table = table('Size', [0,7], 'VariableNames', ...
{'name', 'window', 'fs', ...
'x', 'y', 'z', 'mag'}, ...
'VariableTypes', ...
{'string', 'double', 'double', ...
'double', 'double', 'double', 'double'});
The variable that I want to put in in 'window' is actually a 1x2 double. But when I set it:
my_table.window(1) = [1 4];
I get the error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.
So: how do I specify that my 'window' is actually 1x2?
Many thanks

採用された回答

Steven Lord
Steven Lord 2021 年 7 月 11 日
my_table = table('Size', [0,7], 'VariableNames', ...
{'name', 'window', 'fs', ...
'x', 'y', 'z', 'mag'}, ...
'VariableTypes', ...
{'string', 'double', 'double', ...
'double', 'double', 'double', 'double'});
my_table.window(1, 1:2) = [1 4]
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
my_table = 1×7 table
name window fs x y z mag _________ ______ __ _ _ _ ___ <missing> 1 4 0 0 0 0 0

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2021 年 7 月 11 日
編集済み: Walter Roberson 2021 年 7 月 11 日
You cannot do that using that construction technique.
See also my answer there.

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 11 日
It is better to create a zero matrix and then assign a table variable, e.g.:
Nrows=10; Ncols=8;
A = zeros(10, 8);
my_table = array2table(A, 'VariableNames', {'name', 'win1', 'win2', 'fs','x', 'y', 'z', 'mag'});
%% Assign the values
my_table.win1(1)=1;
my_table.win2(1)=4;
%% Make a new column (var named "window")
my_table.window=[my_table.win1, my_table.win2];
my_table.win1=[];
my_table.win2=[];

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by