Unrecognised function in MATLAB

function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end
>> values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(values)
Unrecognized function or variable 'stat'.
Could anyone tell me what's wrong with this? It is literally MATLAB's 'function with multiple outputs' example.

3 件のコメント

Stephen23
Stephen23 2020 年 10 月 3 日
"Could anyone tell me what's wrong with this?"
Nothing at all is wrong with stat: when I save it in the current directory as a file named stat.m it works perfectly:
>> values = [12.7, 45.4, 98.9, 26.6, 53.1];
>> [ave,stdev] = stat(values)
ave = 47.340
stdev = 29.412
Possibly you did nto save the code as a function, or did not save it on the MATLAB Search Path or current directory. But as you did not explain how and where you saved it, we can only guess.
Vedeesh Bali
Vedeesh Bali 2020 年 10 月 3 日
I saved it as a file, but the output did not change :(
Steven Lord
Steven Lord 2020 年 10 月 3 日
You saved it as a file, but I'm guessing that file was not named stat.m. See the first Note on this documentation page. If you define a function stat inside a file slow.m then you will need to call the function using the name slow not the name stat.

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

回答 (1 件)

Priysha LNU
Priysha LNU 2020 年 10 月 6 日

0 投票

Here is an excerpt from "create functions in files" documentation :
Often, you store a function in its own file. In that case, the best practice is to use the same name for the function and the file (in this example, fact.m), since MATLAB® associates the program with the file name. Save the file either in the current folder or in a folder on the MATLAB search path.
Saving the code to a file named "stat.m" might help you resolve this error.
DISCLAIMER: These are my own views and in no way depict those of MathWorks.

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

質問済み:

2020 年 10 月 3 日

回答済み:

2020 年 10 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by