Saving .mat files in a 'for' loop

조회 수: 123 (최근 30일)
Mitchell
Mitchell 2015년 8월 25일
댓글: SAMSEERA P P 2022년 9월 2일
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
Jy Lee
Jy Lee 2022년 5월 27일
Why save last value? where is whole data?

댓글을 달려면 로그인하십시오.

채택된 답변

Sean de Wolski
Sean de Wolski 2015년 8월 25일
save(['path/X_' num2str(j) '.mat'],'X')
  댓글 수: 7
anuraj uthayasooriyan
anuraj uthayasooriyan 2021년 6월 20일
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