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

3 ビュー (過去 30 日間)
Peter Beringer
Peter Beringer 2020 年 10 月 9 日
コメント済み: Peter Beringer 2020 年 10 月 11 日
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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by