How to output and name variables generated in 'for' loop into workspace.

2 views (last 30 days)
Peter Beringer
Peter Beringer on 9 Oct 2020
Edited: Ramo Rafsel on 23 Oct 2020
Wrote a little function to generate random frequency sine tones. I would like each tone generated to be output into the workspace as a variable with name determined by output and input arguments and randomly generated variables (namely, tone freqency). I figured how to put the generated tones into a cell, but that's not what I want; I just want the simple, discrete audio vectors (1 x Nsamples), etc.
Comment lines in the code below explain what I'm trying to achieve a bit further. I KNOW this is a very simple thing, but the solution escapes me, and I'm not sure where to look in the documentation.
Thanks in advance!
function [Signal] = SineWave(Flower, Fupper, Amp, Fs, Time, NumTones)
% Random frequency sine tone generator ...
% Generates a specified number of sine tones at specified length and
% amplitude with random frequency values within specified upper and lower limits.
for i = 1 : NumTones
Frequency = randi([Flower Fupper]);
t = 0:1/Fs:Time;
Signal = Amp * sin(2*pi*Frequency*t);
sound(Signal, Fs)
pause(Time)
% ADD LINES TO OUTPUT EACH TONE GENERATED AS 'Signal(Frequency)_i'
% Output variables named according to the [Signal] output argument followed by the randomly generated
% frequency value and number (i). For example, for five random sine tones (NumTones), five variables should be outputted named thusly:
% 'Signal250_1' 'Signal440_2' 'Signal1000_3' 'Signal330_4' 'Signal2500_5'
end
  1 Comment
Stephen
Stephen on 10 Oct 2020
"I KNOW this is a very simple thing..."
Not really.
"...but the solution escapes me, and I'm not sure where to look in the documentation"
Start by reading this documentation:
in particular the section that states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 9 Oct 2020
Can you define variables with numbered names like Tone200_1, Tone400_2, etc, ... ? Yes.
  7 Comments
Ramo Rafsel
Ramo Rafsel on 23 Oct 2020
I posted a question about the same topic ( https://www.mathworks.com/matlabcentral/answers/621313-a-loop-for-sorting-tables ) and thanks to @Stephen Cobeldick because he led me to this question. So I thought I ll just add my question here.
I would like to generate one loop or make extracting and sorting different tables by the its cell contents (e. g. sorting by different variable..) somewhat automatic with the loop, so that I do not need to repeat writing the same lines everytime.
I ve tried to do the same thing for the following code, which turns values from tables into plots, since I have to repeat the same lines for plotting more than 15 times, I would like to know if there are other ways which I can use that does extracting data from a table into different tables or plots automatically like a loop or other tools.
I hope i could explain the problem clearly.
In case it is not appropriate to add a question to an already answered one, you can just comment in the link of the question that i added above.
Thanks a lot in advance for the great help.
plot(time_COND9,value_COND9);
xticks([time_COND9(1) time_COND9(end-1)]);
yticks([min(value_COND9) max(value_COND9) ]);
hold on
plot([time_COND9(1) time_COND9(end-1)],[avg_COND9 avg_COND9],'r-','LineWidth',2);
title('H1_ COND_ 09')
xlabel('Uhrzeit in [HH:MM:SS]')
hold off
nexttile
plot(COND9_timefb1(1:end-1),COND9_fb1);
xticks([COND9_timefb1(1) COND9_timefb1(end-1)]);
hold on
plot(COND9_timefb2(2:end-1),COND9_fb2);
plot(COND9_timefb3(3:end-1),COND9_fb3);
title('H1_ COND_ 09')
xlabel('Uhrzeit in [HH:MM:SS]')
ylabel('Frequenzintervall 150kHz - 80 MHz')
hold off
nexttile
plot(time_S15hPa,value_S15hPa);
xticks([time_S15hPa(1) time_S15hPa(end-1)]);
yticks([min(value_S15hPa) max(value_S15hPa) ]);
hold on
plot([time_S15hPa(1) time_S15hPa(end-1)],[avg_S15hPa avg_S15hPa],'r-','LineWidth',2);
title('H1_ PRESSURE_ S15hPa')
xlabel('Uhrzeit in [HH:MM:SS]')
hold off
nexttile
plot(S15hPa_timefb1(1:end-1),S15hPa_fb1);
xticks([S15hPa_timefb1(1) S15hPa_timefb1(end-1)]);
hold on
plot(S15hPa_timefb2(2:end-1),S15hPa_fb2);
plot(S15hPa_timefb3(3:end-1),S15hPa_fb3);
title('H1_ PRESSURE_ S15hPa')
xlabel('Uhrzeit in [HH:MM:SS]')
ylabel('Frequenzintervall 150kHz - 80 MHz')
hold off
nexttile
plot(time_Pt02C,value_Pt02C);
xticks([time_Pt02C(1) time_Pt02C(end-1)]);
yticks([min(value_Pt02C) max(value_Pt02C) ]);
hold on
plot([time_Pt02C(1) time_Pt02C(end-1)],[avg_Pt02C avg_Pt02C],'r-','LineWidth',2);
title('H1_ TEMPERATURE_ Pt02Celsius')
xlabel('Uhrzeit in [HH:MM:SS]')
hold off
nexttile
plot(Pt02C_timefb1(1:end-1),Pt02C_fb1);
xticks([Pt02C_timefb1(1) Pt02C_timefb1(end-1)]);
hold on
plot(Pt02C_timefb2(2:end-1),Pt02C_fb2);
plot(Pt02C_timefb3(3:end-1),Pt02C_fb3);
title('H1_ PRESSURE_ S15hPa')
xlabel('Uhrzeit in [HH:MM:SS]')
ylabel('Frequenzintervall 150kHz - 80 MHz')
hold off
nexttile
plot(time_Pt06C,value_Pt06C);
xticks([time_Pt06C(1) time_Pt06C(end-1)]);
yticks([min(value_Pt06C) max(value_Pt06C) ]);
hold on
plot([time_Pt06C(1) time_Pt06C(end-1)],[avg_Pt06C avg_Pt06C],'r-','LineWidth',2);
title('H1_ TEMPERATURE_ Pt06Celsius')
xlabel('Uhrzeit in [HH:MM:SS]')
hold off
nexttile
plot(time_Pt08C,value_Pt08C);
xticks([time_Pt08C(1) time_Pt08C(end-1)]);
yticks([min(value_Pt08C) max(value_Pt08C) ]);
hold on
plot([time_Pt08C(1) time_Pt08C(end-1)],[avg_Pt08C avg_Pt08C],'r-','LineWidth',2);
title('H1_ TEMPERATURE_ Pt08Celsius')
xlabel('Uhrzeit in [HH:MM:SS]')
hold off
nexttile
plot(time_ST,value_ST);
xticks([time_ST(1) time_ST(end-1)]);
yticks([min(value_ST) max(value_ST) ]);
hold on
plot([time_ST(1) time_ST(end-1)],[avg_ST avg_ST],'r-','LineWidth',2);
title('M1_ SYSTEM_ SystemTime')
xlabel('Uhrzeit in [HH:MM:SS]')
hold off

Sign in to comment.

More Answers (1)

Mohammad Sami
Mohammad Sami on 9 Oct 2020
Edited: Mohammad Sami on 9 Oct 2020
You can store the values in a cell array. Edited portion of your code
if true
Signal{i} = Amp * sin(2*pi*Frequency*t);
sound(Signal{i}, Fs)
end
  3 Comments
Mohammad Sami
Mohammad Sami on 10 Oct 2020
if true
Frequency = randi([Flower Fupper]);
Signal{i,1} = Frequency;
Signal{i,2} = Amp * sin(2*pi*Frequency*t);
sound(Signal{i,2}, Fs);
end
Updated to include frequency in column 1 and signal in column 2

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!