0% found this document useful (0 votes)
5 views10 pages

Itw 1

Basic Syntex

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Itw 1

Basic Syntex

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

IT

Workshop
LECTURE 1
Syntax Basics
Continue Long Statements on Multiple
Lines
This example shows how to continue a statement to the next line using ellipsis (...).

s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...

- 1/6 + 1/7 - 1/8 + 1/9;

Build a long character vector by concatenating shorter vectors together:

mytext = ['Accelerating the pace of ' ...

'engineering and science'];


Name=Value in Function Calls
MATLAB supports two syntaxes for passing name-value arguments.

plot(x,y,LineWidth=2) name=value syntax

plot(x,y,"LineWidth",2) comma-separated syntax

Use the name=value syntax to help identify name-value arguments for functions and to clearly

distinguish names from values in lists of name-value arguments.


Ignore Function Outputs
Request all three possible outputs from the fileparts function.

helpFile = which('help');

[helpPath,name,ext] = fileparts(helpFile);

To ignore function outputs in any position in the argument list, use the tilde operator. For example,

ignore the first output using a tilde.

[~,name,ext] = fileparts(helpFile);

You can ignore any number of function outputs using the tilde operator. Separate consecutive tildes

with a comma. For example, this code ignores the first two output arguments.

[~,~,ext] = fileparts(helpFile);
Variable Names
Examples of valid names:

x6

lastValue

n_factorial

Examples of invalid names:

6x

end

n!
Case and Space Sensitivity
MATLAB code is sensitive to casing, and insensitive to blank spaces except when defining
arrays.

For example, MATLAB interprets the following statements the same way.

y = sin (3 * pi) / 2

y=sin(3*pi)/2

For example, the statement [1 sin (pi) 3] produces a much different result than [1 sin(pi) 3] does.

Error using sin

Not enough input arguments.


Choose Command Syntax or Function
Syntax
MATLAB has two ways of calling functions, called function syntax and command syntax.

In MATLAB, these statements are equivalent:

load durer.mat % Command syntax

load('durer.mat') % Function syntax

This equivalence is sometimes referred to as command-function duality.

All functions support this standard function syntax:

[output1, ..., outputM] = functionName(input1, ..., inputN)


CAVEAT !
With command syntax, MATLAB passes all inputs as character vectors (that is, as if they were

enclosed in single quotation marks) and does not assign outputs to user defined variables.

A = 123;

disp(A)

This code returns the expected result,

123

You cannot use command syntax to pass the value of A


Avoid Common Syntax Mistakes

You might also like