Saving .mat files in a 'for' loop

Hello,
I am trying to save a variable to a path in a for loop multiple times and I also want to save the variable in each loop under a different name.
This is essentially what I am trying to do except for a lot more computation to find X:
for j=1:1000
X=j^2
save('path/X_j.mat','X');
end
This obviously does not work since I cannot put a variable into the save path this way. Is it possible to put the variable j into the save path somehow so that I get an ordered series of .mat files? ie X_1.mat,X_2.mat,....
If it isn't possible to put the variable into the saved name is there some other way to get an ordered set of .mat files saved to the same path?
Thank you.

1 comentario

Jy Lee
Jy Lee el 27 de Mayo de 2022
Why save last value? where is whole data?

Iniciar sesión para comentar.

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 25 de Ag. de 2015
save(['path/X_' num2str(j) '.mat'],'X')

7 comentarios

Prasanth Thangavel
Prasanth Thangavel el 31 de Ag. de 2017
Thanks. Worked like a charm.
ahmad eldeeb
ahmad eldeeb el 11 de Abr. de 2018
thanks
Jui-tse Hung
Jui-tse Hung el 17 de Abr. de 2019
This is really helpful.
Thanks.
It help solve the problems when the dimension of a variable can't exceed 3.
so I can save the variable each time in my for loop.
Especially the weights in Machine Learning.
The weights are 3 dimension in our matrix.
And I want to change different parameters, then training my system again.
I have to save the weights each time.
This technique is really helpful.
Brett Wood
Brett Wood el 2 de Mayo de 2019
Thank you!!! This is so much more simple and intuitive than other methods I have thus far seen.
Vasilis Mitropoulos
Vasilis Mitropoulos el 22 de Sept. de 2019
THANKS MAAAAN; SOOO SIMPLE AND NICE:)
SAVED ME A FEW HOUR FROM MY LIFE!!!
anuraj uthayasooriyan
anuraj uthayasooriyan el 20 de Jun. de 2021
Helpful for me as well. Thank you very much.
SAMSEERA P P
SAMSEERA P P el 2 de Sept. de 2022
thank yu so much....it worked for me too

Iniciar sesión para comentar.

Más respuestas (1)

Osama Muhammad
Osama Muhammad el 11 de Nov. de 2019

0 votos

Sine_Loop is below
Where a blank is shown you must use correct English and simple statements to convey your answer. 10 points.
Open the Matlab file Sine_Loop in Blackboard. Save it to Documents\Matlab folder.
1. What is the last value of the output variable ‘result’______________?
2. How many sine waves are produced by this Matlab routine _______? What do they look like? Describe them in one sentence (If you write 2 sentences, no credit is given)_____________________________________________________________________________ _______________________________________________________________________.
3. What happens when line 5 is executed? ______________________________________________________________________________ _________________________________________________________________________?
4. In MATLAB, a for loop begins with the statement indicating how many times the statements in the loop will be executed. This statement is called a _____________. The _____________ will be incremented by a specific value until its final value is reached (stop value). Therefore, the loop will be executed the number of times according to the ____________.
5. Circle the correct answer and fill in the blank: When I look at the plots, the I see the waveform’s amplitude does or does not change while the _________________ changes as each note is played.
Sine_Loop File code
1. A = 4;
2. samplerate=4000 %needs to be less than half of the frequency, F
3. for F = 100:100:300 %three frequencies
4. t = 0:1/samplerate:0.1; %this is the time variable
5. result = A*sin(2*pi*F*t) %compute result by the sine function (the loop will compute 3 results)
6. plot(2*pi*F*t,result)%plot the result on the y axis and time on x-axis
7. xlabel('time(s)');%label the x-axis
8. ylabel('Amplitude (units)'); %naming axis
9. sound(result,samplerate); %sounding the output signal at a sample rate of 8000 Hertz
10. pause(3); %pause between each loop
11. % the loop and sine wave computation keeps going for each Frequency
12. % 100, 200, 300
13. end %ending of loop

6 comentarios

Walter Roberson
Walter Roberson el 11 de Nov. de 2019
What is your question?
Osama Muhammad
Osama Muhammad el 11 de Nov. de 2019
Can you help with answering these question I'm stuck
Osama Muhammad
Osama Muhammad el 11 de Nov. de 2019
The fill in the blank by looking at the code which is at the bottom
Walter Roberson
Walter Roberson el 11 de Nov. de 2019
When you ran the code, there would have been a final value stored in result. The value stored there needs to be answer given for the first question. However, considering that the answer will be a list of 401 values, perhaps they want a description instead of a list of numeric values.
Osama Muhammad
Osama Muhammad el 11 de Nov. de 2019
Can you answer the rest of them please
Walter Roberson
Walter Roberson el 11 de Nov. de 2019
No. In #4 that part "This statement is called a _____________." has no answer in MATLAB. There is no name for the statement in MATLAB. There are names for similar statements in other languages, but those languages are not MATLAB.
The answer to #5 depends upon arguments about whether or not you are talking about a group property or a property individual samples when you refer to "waveform". There is no "right" answer to the question because it uses terms that have not been sufficiently defined.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 25 de Ag. de 2015

Comentada:

el 2 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by