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

9 Ansichten (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 18 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 18 Feb. 2021
Bearbeitet: Matt J am 18 Feb. 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 Kommentar
Dave
Dave am 18 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by