Asked by Mitchell
on 25 Aug 2015

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.

Answer by Sean de Wolski
on 25 Aug 2015

Accepted Answer

save(['path/X_' num2str(j) '.mat'],'X')

Jui-tse Hung
on 17 Apr 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
on 2 May 2019

Thank you!!! This is so much more simple and intuitive than other methods I have thus far seen.

Vasilis Mitropoulos
on 22 Sep 2019

THANKS MAAAAN; SOOO SIMPLE AND NICE:)

SAVED ME A FEW HOUR FROM MY LIFE!!!

Sign in to comment.

Answer by Osama Muhammad
on 11 Nov 2019 at 3:21

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

Walter Roberson
on 11 Nov 2019 at 4:14

Osama Muhammad
on 11 Nov 2019 at 4:16

Can you answer the rest of them please

Walter Roberson
on 11 Nov 2019 at 4:53

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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.