How to output and name variables generated in 'for' loop into workspace.
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2020-10-10
"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."
采纳的回答
Steven Lord
2020-10-9
Can you define variables with numbered names like Tone200_1, Tone400_2, etc, ... ? Yes.
6 个评论
Stephen23
2020-10-10
"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).
更多回答(1 个)
Mohammad Sami
2020-10-9
编辑:Mohammad Sami
2020-10-9
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 个评论
Mohammad Sami
2020-10-10
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!