How can I save variables from the loop?

I have a seismic data for each day from which I need to perform some calculation to recieve average amplitude for specific day. I have 240 such columns/days with 144000 amplitute datas. I am interested in the frecuency 4-14 Hz to find out the human activity seismic noise from that.
How can I make a 'for; loop to get this data calulated for the rest 240 day so that I also recieve/have it saved in matlab file an average amplitude of tyat speciefic day? I will have to plot this data against the days later on to see how the amplitude has changed with time.
The calculation for one day looks like this:
[amp,freq]=periodogram(detrend(seis(:,1)),[],[],Fs) %using periodogram I extract the amplitude and frequency of a data
[Fr,Fc]=find(freq>=4 & freq<=14) %Get row and columns for which data between 4-14 Hz is true
plot(freq(Fr),amp(Fr)) %Plot the frequency and amplitude that fit 4-14 Hz range
av1=mean(amp(Fr)) %find the average amplitude that fits the range 47-14 Hz
%I need to have it calculated so that I get something like this for tyhe
%second day:
[amp2,freq2]=periodogram(detrend(seis(:,2)),[],[],Fs)
loglog (freq2, amp2)
[amp2,freq2]=periodogram(detrend(seis(:,2)),[],[],Fs)
[Fr2,Fc2]=find(freq2>=4 & freq2<=14)
plot(freq2(Fr2),amp2(Fr2))
av2=mean(amp2(Fr2))
af2=mean(freq2(Fr2))
%For the third day and so on:
[amp3,freq3]=periodogram(detrend(seis(:,3)),[],[],Fs)
loglog (freq3, amp3)
[amp3,freq3]=periodogram(detrend(seis(:,3)),[],[],Fs)
[Fr3,Fc3]=find(freq3>=4 & freq3<=14)
plot(freq3(Fr3),amp2(Fr3))
av3=mean(amp2(Fr3))
af3=mean(freq2(Fr3))

1 Comment

What is the relation between the question and the code? Over which value do you want to iterate the loop and which output should be collected?

Sign in to comment.

Answers (1)

% program before that, then
fieldname = "avg_results_" + day_number;
temp = struct(fieldname, av3);
save("YourFileOfResults.mat", "-struct", "temp", "-append");
This would create a new variable avg_results_### inside YourFileOfResults.mat
You might also want to have a look at matfile()
... But most of the time it is better to just accumulate all of the results and save them all at the end. You can probably pre-allocate the array of results, and it probably is not going to be very big.

Categories

Find more on Seismology in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 26 Mar 2022

Answered:

on 26 Mar 2022

Community Treasure Hunt

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

Start Hunting!