フィルターのクリア

How to store value in the table using looping

22 ビュー (過去 30 日間)
Yew Jen Chong
Yew Jen Chong 2022 年 6 月 22 日
コメント済み: Yew Jen Chong 2022 年 6 月 24 日
Hi,
I want to store data in a table using looping. I am trying to store all the value of each iteration in the table but it can only store the values of the last iteration. The coding below is what I tried.
for n = 1:3
Table.Data_A = DataA;
Table.Data_B = DataB;
Table.Data_C = DataC;
Table(n,;) = Table;
end
I will appreciate if someone can help me.
Thank you.

採用された回答

Karim
Karim 2022 年 6 月 22 日
編集済み: Karim 2022 年 6 月 23 日
hello, below you can find an example on how to store data in a table in a loop
numLoop = 10; % indicate the number of loops...
numCol = 3; % indicate the number of columns for the table...
% generate some random data
DataA = 1:numLoop;
DataB = rand(numLoop,1);
DataC = ["A","B","C","D","E","F","G","H","I","J"];
% allocate the table
varTypes = ["double","double","string"]; % data type for each column
varNames = ["DataA","DataB","DataC"]; % variable name for each column
MyTable = table('Size',[numLoop numCol],'VariableTypes',varTypes,'VariableNames',varNames);
% perfom the loop
for n = 1:numLoop
% store data in the table
MyTable(n,:) = {DataA(n), DataB(n), DataC(n)};
end
% display the result
MyTable
------------------------------------------------------------------------------------------------------------------------------------------------------
Edit: next to the orginal example, i also added modified the code with the later added example data and script, see attachment
@Yew Jen Chong: the code is to slow to run here, however below you can find the print screens of the result run locally (the code is in attachement)
and the remainder of the table...
  1 件のコメント
Yew Jen Chong
Yew Jen Chong 2022 年 6 月 24 日
@Karim Thank you so much for helping me

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

その他の回答 (1 件)

Yew Jen Chong
Yew Jen Chong 2022 年 6 月 23 日
Hi @KASR,
There are some errors when I tried on my side. Would you like to help me correct my mistake?
I have attached the files.
Thank you so much for your help.
  1 件のコメント
Karim
Karim 2022 年 6 月 23 日
see the changes to the original answer, you can comment there ;) No need to create a new 'answer'

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by