Can I use 'table' as a variable type in a table

34 ビュー (過去 30 日間)
Ronald Stahl
Ronald Stahl 2025 年 2 月 26 日 0:49
編集済み: Stephen23 約8時間 前
Help for 'table' says that I can use 'table' as a variable type in a table. I receive the error msg;
"The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 0"
when trying to run the following code.
InnerTable = table('Size',[1 2],'VariableTypes',{'double', 'double'});
InnerTable{2,1} = 14.34;
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.
InnerTable{3,2} = 45;
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.
Table = table('Size',[1 3],'VariableTypes',{'table', 'double', 'struct'});
Table{1,1} = InnerTable;
Error using {} (line 161)
The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 0.
Can a table be assigned to a location within a table? How?
  4 件のコメント
Ronald Stahl
Ronald Stahl 約11時間 前
Thank you for the detailed explanation. I was expecting (hoping) that a table of any size could be assigned to a single cell in another table. Clearly not. Time to approach my problem another way.
Stephen23
Stephen23 約10時間 前
編集済み: Stephen23 約8時間 前
"I was expecting (hoping) that a table of any size could be assigned to a single cell in another table. Clearly not. Time to approach my problem another way."
Tables do not have "cells". Every variable/column of a table is simply an array, which means there is absolutely nothing stopping you from using a cell array as one of those columns, if you need. Then you can allocate a nested table of any size to that variable (i.e. cell array) of the parent table. But do not confuse tables themselves with "cells" (which they definitely do not have).

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

採用された回答

Voss
Voss 約19時間 前
InnerTable = table('Size',[1 2],'VariableTypes',{'double', 'double'});
InnerTable{2,1} = 14.34;
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.
InnerTable{3,2} = 45
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.
InnerTable = 3x2 table
Var1 Var2 _____ ____ 0 0 14.34 0 0 45
Table = table('Size',[height(InnerTable) 3],'VariableTypes',{'table', 'double', 'struct'})
Table = 3x3 table
Var1 Var2 Var3 _________ ____ __________ 1x0 table 0 1x1 struct 1x0 table 0 1x1 struct 1x0 table 0 1x1 struct
% 4 alternatives:
Table.(1) = InnerTable;
Table.Var1 = InnerTable;
Table{:,1} = InnerTable;
Table{:,'Var1'} = InnerTable;
Table
Table = 3x3 table
Var1 Var2 Var3 _____________ ____ __________ Var1 Var2 _____ ____ 0 0 0 1x1 struct 14.34 0 0 1x1 struct 0 45 0 1x1 struct
  2 件のコメント
Ronald Stahl
Ronald Stahl 約11時間 前
Thank you for the detailed explanation. The limits on using a table within a table make this the wrong approach for solving my coding problem.
Voss
Voss 約9時間 前
編集済み: Voss 約9時間 前
You're welcome!
Consider using a cell array for any table variable that needs to be able to contain anything, as Stephen23 suggested above.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by