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

조회 수: 4 (최근 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
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
Steven Lord 2020년 10월 9일
Can you define variables with numbered names like Tone200_1, Tone400_2, etc, ... ? Yes.
  댓글 수: 6
Stephen23
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).
Peter Beringer
Peter Beringer 2020년 10월 11일
Thanks for clarifying that. I've had no issue getting it to work where it generates a cell with each signal's frequency in the row below.
So, taking the generous recommendations by you and others, I've since adjusted the code for which the test tones are intended to call the data - signals and their frequency labels - from the cell using indexing.
Its inefficiency and instability actually makes more sense to me now when considering it from the point of view of it being an attempt to view a datum in the name of a variable.
Thanks again for all your help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mohammad Sami
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
Peter Beringer
Peter Beringer 2020년 10월 9일
Hi, thanks for answering. In my post I mentioned that I'd already figured out how to get the outputs into a cell array (using lines identical to your suggestion, in fact), but I actually want individual, discrete vectors outputted to the workspace. So instead of a signle cell array variable that contains all the tones generated, I would like a different variable in the workspace for each tone (i.e. 'Tone200_1' 'Tone400_2', and so on.
Mohammad Sami
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 CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by