Saving .mat files in a 'for' loop

128 次查看(过去 30 天)
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.

采纳的回答

Sean de Wolski
Sean de Wolski 2015-8-25
save(['path/X_' num2str(j) '.mat'],'X')
  7 个评论
anuraj uthayasooriyan
Helpful for me as well. Thank you very much.
SAMSEERA P P
SAMSEERA P P 2022-9-2
thank yu so much....it worked for me too

请先登录,再进行评论。

更多回答(1 个)

Osama Muhammad
Osama Muhammad 2019-11-11
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 个评论
Osama Muhammad
Osama Muhammad 2019-11-11
Can you answer the rest of them please
Walter Roberson
Walter Roberson 2019-11-11
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by