How to pix the syntax error?

조회 수: 13 (최근 30일)
Hai Ninh
Hai Ninh 2018년 12월 5일
댓글: Walter Roberson 2019년 2월 17일
Hi,
I have proplem with syntax:
function [TDOA, FDOA] = CAF(S1, S2;1500;1e6; 10e-4);
The error is:
  • Invalid syntax at ';' . possibly ,a ')' is missing
  • Parse error at ')': usage might be Invalid MATLAB syntax
How to pix them, thank you so much

답변 (3개)

Walter Roberson
Walter Roberson 2018년 12월 5일
편집: Walter Roberson 2018년 12월 7일
function definition statements must start with the word function (case sensitive so Function is not permitted )
After function there is an optional list of output variables .
A list of output variables can be a single variable name or it can be [ followed by a comma separated list of variable names followed by ] . If there was any output variable then after the single variable or list of variables there must be a = (equals sign)
Only pure variables names can be used for output variables , with no indexing of any kind. No () or {} or dot indexing .
~ is not permitted as an output variable name in a function definition .
After that is the function name which is mandatory . Only pure variable names are permitted as function names.
After the function name there is optional list of input variables . This is a ( followed by a comma separated list of input variables followed by a )
Input variable names must be either ~ or else a pure variable name. No indexing of any kind is permitted .
Your code attempts to use semicolon between input variable names and also attempts to use numbers in place of input variable names .
There is no way in MATLAB to specify default values for variables in a function definition .
The list of input variables can end with the special name varargin . This special name captures all remaining input arguments into a cell array that can be accessed with regular cell notation . This permits an indefinite number of arguments to a function .

madhan ravi
madhan ravi 2018년 12월 5일
편집: madhan ravi 2018년 12월 5일
Read more about functions()
function [TDOA, FDOA] = CAF(S1, S2,1500,1e6, 10e-4);
% ^---^----^---^-------- commas not semicolon
  댓글 수: 2
krishna sudeep
krishna sudeep 2019년 2월 17일
Can you provide source code for this?
Walter Roberson
Walter Roberson 2019년 2월 17일
function [TD0A, FD0A] = CAF(S1, S2, S3, S4, S5)
if ~exist('S3', 'var'); S3 = 1500; end
if ~exist('S4', 'var'); S4 = 1e6; end
if ~exist('S5', 'var'); S5 = 10E-4; end

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2018년 12월 5일
It looks like you're confusing defining a function with calling a function.
The input arguments you specify in a function definition must be valid variable names or varargin separated by commas. S1 and S2 are valid varaible names separated by commas while 1500 is not a valid variable name.
When you call the function you can specify values for the function and those values can be variables, literal numbers, or expressions.
As a simple example, the definition of the why function states that its input argument will be stored in a variable named n inside the workspace of the why function.
>> dbtype 1 why.m
1 function why(n)
When you call why you can specify a number in that call.
>> why(42)
To satisfy the tall and good not excessively terrified mathematician.
You can specify a variable.
>> x = 99;
>> why(x)
Don't ask!
You can specify an expression.
>> why(ceil(exp(4)))
They knew it was a good idea.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by