1 view (last 30 days)

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

Steven Lord
on 9 Oct 2020

Can you define variables with numbered names like Tone200_1, Tone400_2, etc, ... ? Yes.

Stephen Cobeldick
on 10 Oct 2020

"is how to include the frequency/random number in the variable names"

Everyone on this thread has already recommended that you do NOT do that.

Meta-data (such as frequencies) is data, and data belongs in variables, not in variable names.

"but as I won't know the frequency, I can't call the variable by its name."

That is one of the reasons why your approach should be avoided, and is why everyone is recommending that you use better data design (i.e. storing the data in one or two variables).

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

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

Mohammad Sami
on 9 Oct 2020

Similarly you can store the corresponding frequency for each signal in another array.

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

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/609206-how-to-output-and-name-variables-generated-in-for-loop-into-workspace#comment_1050126

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/609206-how-to-output-and-name-variables-generated-in-for-loop-into-workspace#comment_1050126

Sign in to comment.