Need help creating a variable with different names or a method for saving a variable to different names.

11 vues (au cours des 30 derniers jours)
Hello all, i need to find a way/method to either create a dynamic variable within my loop to store data that labels the array depending on the loops iteration number or save that varible to a dynamically named .mat file.
At the moment i am taking multiple data from a sensor then store the data in a 4 x 4 matrix's named A, B, C, D.
A(i,j) = readline(s);
B(i,j) = readline(s);
C(i,j) = readline(s);
D(i,j) = readline(s);
However, once those array is filled, i either need to save the arrays to a .mat file such as A0, B0, C0, D0 then proceed to the data storing process OR for the next iteration create another set of variables to be A1,B1, C1, and D1.
Scenario 1,
I need to a way inside of my loop to create variables depending on the current loops iteration to save data too, so for loop 0, variable A0, B0, C0, D0, then for iteration 1, variables A1, B1, C1, D1.
For example, loop 0:
A0(i,j) = readline(s);
B0(i,j) = readline(s);
C0(i,j) = readline(s);
D0(i,j) = readline(s);
then loop 1;
A1(i,j) = readline(s);
B1(i,j) = readline(s);
C1(i,j) = readline(s);
D1(i,j) = readline(s);
etc...
A#(i,j) = readline(s);
B#(i,j) = readline(s);
C#(i,j) = readline(s);
D#(i,j) = readline(s);
OR if the above scenario is not possible, i would like a way to save the data variable to a mat file, with dynamic nameing based also on the interation.
Scenario 2,
For loop 0, i would like to save the variables of A, B, C, D to something like A0, B0, C0, and D0. Then for loop 1, A, B, C, D would be saved as A1, B1, C1, D1, and so forth.
Thank you in advance for any help and responses.
  1 commentaire
Stephen23
Stephen23 le 18 Fév 2021
"i would like a way to save the data variable to a mat file, with dynamic nameing based also on the interation."
Note that processing data from multiple .mat file is much simpler when the variable names do not change.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 18 Fév 2021
Modifié(e) : Matt J le 18 Fév 2021
for k=1:K
S(k).A(i,j) = readline(s);
S(k).B(i,j) = readline(s);
S(k).C(i,j) = readline(s);
S(k).D(i,j) = readline(s);
end
  1 commentaire
Dave
Dave le 18 Fév 2021
Hey @Matt J, thank you for replying. Never used structures before or actually knew about it, but that seems to be a solution to my problem. I will look more into structures to see how i can adapt it to what i need.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by