How to define a function in the command window?
130 次查看(过去 30 天)
显示 更早的评论
I'm trying to define the function
t=1:1/8000:5
x(n)=sin(2*pi*t(n))
I've tried function x(n)=sin(2*pi*t(n)) like you would do in the editor but it doesn't work. What's the correct sintax?
0 个评论
回答(3 个)
Walter Roberson
2012-10-4
At the command window, you can use
x = @(n) sin(2*pi*t(n));
This will create the function named "x" that takes a single parameter, n.
Note that this function will not be available within other functions unless it is passed as a parameter to the other function: its definition will be local to the scope it is in.
Jan
2012-10-4
Functions can be defined in M-files only. In addition these M-files must be functions itself, so scripts are not allowed. And an M-file is a function, if it starts with the term "function".
Reading the Getting Started chapters of the documentation is obligatory, when you want to use such a powerful tool as Matlab.
2 个评论
Jan Kappen
2024-7-24
lol, this is 12 years old and has changed.
Functions can now be defined almost anywhere in scripts and function files. See Types of Functions - MATLAB & Simulink (mathworks.com)
Wayne King
2012-10-4
t=1:1/8000:5;
x =sin(2*pi*t);
Have you tried to read the MATLAB documentation?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!