Functions Flashcards

1
Q

Returning variables in a function

A

Function x=’function name’

//commands to execute

End

Where x is the variable in the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Returning multiple variables in a function

A

Function [x y z …] = ‘function name’

//commands to execute

End

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to pass a variable(s) to functions from the command line

A

Function x=’function name’(y,z,a,b,…)

//commands to execute

End

Where y,z,a,b are variables that are passed to the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Structure of a function in MATLAB

A

Function ‘function name’

//commands to perform

End

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Functions and sub functions accessibility in a single .m file

A

Only the function placed at the top of the .m file can be accessed at the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Creating global variables

A

Global ‘variable name’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Pause MATLAB for a period of n seconds

A

Pause(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does max(variable/array/matrix) return

A

Max(variable/array/matrix) can return the max number and the position of the max number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Generating random integer numbers

Generating random numbers with mean of 0 and standard deviation of 1

A

Integer= randi(integer range,size)

Normal distribution= randn(size)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to Change a variable to fixed point

A

Fix(variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Generating zeros of a certain size
Generating ones of a certain size
Generating integers of a certain size

A
Zeros = zeros(size)
Ones = ones(size)
Integers = integer*ones(size)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the return command in a function do

A

Stops the function from further executing and returning to the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Purpose of nargin function for creating polymorphic functions

A

Returns number of actual input arguments given to the function

Eg if function is given 3 input arguments, nargin returns a value of 3

Function ‘function name’(x,y)

If ‘function name’(3,2) is called then nargin returns a value of 2 when used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Purpose of nargout function for creating polymorphic functions

A

returns the number of output arguments requested by the function caller

Eg. If function called as [x y] = ‘function name’
Then nargout returns a value of 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the correct operator to use for 5 AND 6
What is the correct operator to use for 2 OR -2
What are these operators called
What is the correct operator to use for 5 AND [1 2 3]
What is the correct operator to use for 2 OR [-2 0 7]
What are these operators called

A
  1. &&
  2. ||

Scalar logical operators

  1. &
  2. |

Array logical operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Checking if a value is a scalar value

Checking if a variable is empty

A
  1. Isscalar(value)

2. Isempty(variable)

17
Q

Creating variables that will maintain its contents each time a function is called

What does this variable initialize to

A

Persistent ‘variable name’

Persistent variables does not get cleared when a function is completed
It also initializes to an empty matrix on the first load

18
Q

Clearing a persistent variable in a function

A
  1. Clear ‘function name’
  2. Exit and reopen matlab
  3. Saving the function
19
Q

How to create sections in a script

A

Add the %% at the start of each desired mew section

20
Q

How to make a function display error messages

A

Use the error(‘message’)

Error message automatically creates a new line

21
Q

How to create a for loop

A

For x = y:z

//operations to execute

End

22
Q

How to create a while loop

A

While //condition to meet not met

//operations to execute

End

23
Q

Function to check name, size, byte and type of a variable or all variables

A

Whos ‘variable name’

Whos

24
Q

What to add to skip an output argument

Eg for the max function

A

Replace the output argument with a ~

Max function is polymorphic, can take 2 output arguments. But if say, only the 2nd argument is desired, the 1st output argument can be skipped by using the ~ function

Eg [~ x] = max([1 2 3 4 5])

This will only return the location of the max value

25
Q

Checking if 2 variables are equal

What does it do, what does it return

A

Isequal(x,y)

Compares entire of x vs y instead of individual data in x & y

Returns 1 if equal