How to store value in the table using looping

20 vues (au cours des 30 derniers jours)
Yew Jen Chong
Yew Jen Chong le 22 Juin 2022
Commenté : Yew Jen Chong le 24 Juin 2022
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.

Réponse acceptée

Karim
Karim le 22 Juin 2022
Modifié(e) : Karim le 23 Juin 2022
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 commentaire
Yew Jen Chong
Yew Jen Chong le 24 Juin 2022
@Karim Thank you so much for helping me

Connectez-vous pour commenter.

Plus de réponses (1)

Yew Jen Chong
Yew Jen Chong le 23 Juin 2022
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 commentaire
Karim
Karim le 23 Juin 2022
see the changes to the original answer, you can comment there ;) No need to create a new 'answer'

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by