calling a function: "undefined function or variable 'abc'"

1 回表示 (過去 30 日間)
Thorsten Zwinger
Thorsten Zwinger 2017 年 1 月 5 日
コメント済み: Niels 2017 年 1 月 6 日
Hi there,
I'm trying to write my very first function and it looks like this:
function noiseSignal = add_white_noise(t_spalte, recSignal)
noiseSignal = recSignal + 1/64*randn(size(t_spalte));
end
I call the function with name and input via the command line and I get the noise on my original signal, but not under the variable "noiseSignal". I can use it via ans, but that's it. How can I save the noisy signal under "noiseSignal" for further usage?
Thanks a lot!

採用された回答

Stephen23
Stephen23 2017 年 1 月 6 日
編集済み: Stephen23 2017 年 1 月 6 日
You are getting confused by the variable names that you have used inside the function. These are irrelevant. It does not matter at all what names variables have inside the function, because inside the function is a different workspace to where you are calling it. You specify the name of the output variable when the function is called. For example:
function ns = add_white_noise(t_spalte, recSignal)
ns = recSignal + 1/64*randn(size(t_spalte));
end
can be called like this:
>> noiseSignal = add_white_noise(4,[1,2,3,4])
noiseSignal =
0.98118 1.98118 2.98118 3.98118
You might like to read this:
And note that how to call functions is also covered in the introductory tutorials, which are highly recommended for all beginners:
  2 件のコメント
Thorsten Zwinger
Thorsten Zwinger 2017 年 1 月 6 日
Ah, that was it! Thanks a lot! I didn't know I have to call it like this:
>> noiseSignal = add_white_noise(t_spalte,recSignal);
Up till now, I wrote only
>> add_white_noise(t_spalte,recSignal);
Okay, I will look over the introductions. :)
Niels
Niels 2017 年 1 月 6 日
can you see now the difference in out typing?

サインインしてコメントする。

その他の回答 (1 件)

Niels
Niels 2017 年 1 月 5 日
Hi,
if you type
a = add_white_noise(Argument1, Argument2)
the output will be saved within the variable named a. i guess you want its name to be noiseSignal, so type
% Argument1=t_spalte;
% Argument2=recSignal;
noiseSignal = add_white_noise(Argument1, Argument2)
  2 件のコメント
Thorsten Zwinger
Thorsten Zwinger 2017 年 1 月 6 日
Yes, I thought so too and did that. (correct me if I'm wrong, but I don't see any differences between your typing and mine)
The problem is: Matlab won't save the output in the variable, only under ans.
Stephen23
Stephen23 2017 年 1 月 6 日
編集済み: Stephen23 2017 年 1 月 6 日
@Thorsten Zwinger: please show us exactly how you are calling this function. Please make a comment and copy-and-paste the code that you use for calling this function.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeModulation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by