How to define a function in the command window?

조회 수: 23 (최근 30일)
Pedro
Pedro 2012년 10월 4일
댓글: Jan Kappen 2024년 7월 24일
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?

답변 (3개)

Walter Roberson
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
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
Allen
Allen 2024년 2월 11일
편집: Allen 2024년 2월 11일
Which part of the numerous Getting Started chapters may that be?
Be more specific... use SEER - State, Elaborate, Eample, Reiterate. Provide links or clearly indicate where to look. We all have to learn at one point in time.
Jan Kappen
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
Wayne King 2012년 10월 4일
t=1:1/8000:5;
x =sin(2*pi*t);
Have you tried to read the MATLAB documentation?

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by