フィルターのクリア

How do I define an empty table of unknown size?

52 ビュー (過去 30 日間)
Fabrice Lambert
Fabrice Lambert 2020 年 5 月 26 日
編集済み: Fabrice Lambert 2020 年 5 月 26 日
Hi,
I want to define an empty table to fill in a for loop. The size of the table is defined by another variable. How do I define the "VariableType" variable without knowing how many variables there are? In my case they will all be strings. Example:
T = table('Size', [2, numel(Elements)], 'VariableType',["string","string","string","string","string"]);
In this particular cae, numel(Elements) would be 5. But how do I define the table if I don't know numel(Elements) ?

採用された回答

Steven Lord
Steven Lord 2020 年 5 月 26 日
Elements = 1:5;
n = numel(Elements);
T = table('Size', [2 n], 'VariableTypes', repmat("string", 1, n))

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 5 月 26 日
Sorry, variables that do not exist do not have any datatype.
As you add variables to the table, the datatype associated with the variable will be the determined by the datatype of the value being assigned. If necessary, string() the value you are assigning into the table.
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 5 月 26 日
That is not an empty table with an unknown number of variables: that is a table with 2 rows and a known number of variables.
Fabrice Lambert
Fabrice Lambert 2020 年 5 月 26 日
編集済み: Fabrice Lambert 2020 年 5 月 26 日
In the example I gave, the number of variables is known for clearness' sake, yes. In practice "Elements" can be of variable length, so I need code that avoids fixed numbers of variables.

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

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by