Assign an empty table

837 ビュー (過去 30 日間)
Birsen
Birsen 2017 年 4 月 25 日
コメント済み: Amos 2023 年 6 月 29 日
Hi,
I am trying to assign an empty table (lets say "groupData" to be used later in the for loop. In the loop I use another table and fill the groupData. Later on I would like to empty the table (groupData) and refill again and so. I tried the following code , did not work.
I got the error o convert a table to numeric, use the TABLE2ARRAY function. Is there a way to assign an empty table without using table2array, because I use the groupData data after the for loop to get the data with their variable names. Like
if (groupData.Node(i)==4000)&& (groupData.Cell(i+1)==11)
Thank you
Birsen
Here is the code
==============
for h=1:3
k=newK;
m=1;
groupData(:,:)=0
while pData.Node(k)~=4000
groupData(m,:)=pData(k,:)
k=k+1;
m=m+1;
end
newK=k+1;
end

採用された回答

Birsen
Birsen 2017 年 4 月 26 日
編集済み: Walter Roberson 2017 年 4 月 26 日
I think I resolved it as follows:
groupData = array2table(zeros(0,9));
groupData.Properties.VariableNames = {'Node','x2', ...
'x3','Cell', 'x5','x6','x7', 'x8', 'x9'}
Just defining groupData = array2table(zeros(0,9)) was not sufficient. I had to also use
groupData.Properties.VariableNames = {'Node','x2', ...
'x3','Cell', 'x5','x6','x7', 'x8', 'x9'}
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 8 日
>> groupData = array2table(zeros(0,9))
groupData =
0x9 empty table
MATLAB believes that it is an empty table.
I think you failed to note that the first parameter to zeros is 0, which causes a variable with no rows to be created. Remember that a variable is considered empty if any of its dimensions is 0.
Amos
Amos 2023 年 6 月 29 日
... as a single statement + alternative zero-height array syntax
groupData = array2table( double.empty(0,9) ...
, 'VariableNames' ...
, {'Node','x2','x3','Cell', 'x5','x6','x7', 'x8', 'x9'} ...
)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 4 月 25 日
If the idea is that you want to write 0 over top of all of the existing entries, then
groupData{:,:} = 0;
If you want to remove all of the entries, then
groupData(:,:) = [];
  2 件のコメント
Birsen
Birsen 2017 年 4 月 25 日
Walter thank you for the answer. But both gave an error I tried groupData{:,:} , got the following error "The following error occurred converting from table to cell: Conversion to cell from table is not possible".
And tried groupData(:,:) = []; got error too. "Deletion requires an existing variable".
Walter Roberson
Walter Roberson 2017 年 4 月 26 日
>> groupData = array2table(rand(5,2))
groupData =
5×2 table
Var1 Var2
_________________ _________________
0.929021451353742 0.945085903751417
0.723183836546625 0.705712522061558
0.346528149686704 0.801252732520179
0.453861726186205 0.680579929444058
0.993489506713988 0.784894685163496
>> groupData{:,:} = 0;
>> groupData
groupData =
5×2 table
Var1 Var2
____ ____
0 0
0 0
0 0
0 0
0 0

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by