How to pix the syntax error?

13 views (last 30 days)
Hai Ninh
Hai Ninh on 5 Dec 2018
Commented: Walter Roberson on 17 Feb 2019
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

Answers (3)

Walter Roberson
Walter Roberson on 5 Dec 2018
Edited: Walter Roberson on 7 Dec 2018
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 on 5 Dec 2018
Edited: madhan ravi on 5 Dec 2018
Read more about functions()
function [TDOA, FDOA] = CAF(S1, S2,1500,1e6, 10e-4);
% ^---^----^---^-------- commas not semicolon
  2 Comments
krishna sudeep
krishna sudeep on 17 Feb 2019
Can you provide source code for this?
Walter Roberson
Walter Roberson on 17 Feb 2019
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

Sign in to comment.


Steven Lord
Steven Lord on 5 Dec 2018
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.

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!