Functions Flashcards
Returning variables in a function
Function x=’function name’
//commands to execute
End
Where x is the variable in the function
Returning multiple variables in a function
Function [x y z …] = ‘function name’
//commands to execute
End
How to pass a variable(s) to functions from the command line
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
Structure of a function in MATLAB
Function ‘function name’
//commands to perform
End
Functions and sub functions accessibility in a single .m file
Only the function placed at the top of the .m file can be accessed at the command line
Creating global variables
Global ‘variable name’
Pause MATLAB for a period of n seconds
Pause(n)
What does max(variable/array/matrix) return
Max(variable/array/matrix) can return the max number and the position of the max number
Generating random integer numbers
Generating random numbers with mean of 0 and standard deviation of 1
Integer= randi(integer range,size)
Normal distribution= randn(size)
How to Change a variable to fixed point
Fix(variable)
Generating zeros of a certain size
Generating ones of a certain size
Generating integers of a certain size
Zeros = zeros(size) Ones = ones(size) Integers = integer*ones(size)
What does the return command in a function do
Stops the function from further executing and returning to the command line
Purpose of nargin function for creating polymorphic functions
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
Purpose of nargout function for creating polymorphic functions
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
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
- &&
- ||
Scalar logical operators
- &
- |
Array logical operators
Checking if a value is a scalar value
Checking if a variable is empty
- Isscalar(value)
2. Isempty(variable)
Creating variables that will maintain its contents each time a function is called
What does this variable initialize to
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
Clearing a persistent variable in a function
- Clear ‘function name’
- Exit and reopen matlab
- Saving the function
How to create sections in a script
Add the %% at the start of each desired mew section
How to make a function display error messages
Use the error(‘message’)
Error message automatically creates a new line
How to create a for loop
For x = y:z
//operations to execute
End
How to create a while loop
While //condition to meet not met
//operations to execute
End
Function to check name, size, byte and type of a variable or all variables
Whos ‘variable name’
Whos
What to add to skip an output argument
Eg for the max function
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
Checking if 2 variables are equal
What does it do, what does it return
Isequal(x,y)
Compares entire of x vs y instead of individual data in x & y
Returns 1 if equal