Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MATLAB: Cell Arrays, Functions, and Custom Functions, Study notes of Philosophy of psychiatry

How to use cell arrays in matlab to store different types of data in the same variable and provides an example of creating a 2d cell array. Additionally, it covers the basics of writing custom functions in matlab, including defining inputs and outputs, calculating desired statistics, and plotting data. A specific example of writing a function called 'allstats' that calculates mean, standard deviation, minimum, maximum, and median of an array is given.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-hk1
koofers-user-hk1 🇺🇸

10 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download MATLAB: Cell Arrays, Functions, and Custom Functions and more Study notes Philosophy of psychiatry in PDF only on Docsity! 1 Cell arrays Cells are a form of data structure that allow different types of data types to be saved in the same variable (i.e., numbers, vectors, strings) Braces or curly brackets {} are used instead of [] or () to designate a variable as a cell array Example: c{1}=1; %number or scalar c{2}=’hello’; %string c{3}=1:10; %vector will create a 3-cell array that contains a number, a string, and an array as its entries. When calling back cell arrays, use curly brackets. c{1} c{2} c{3} Could also do create 2, 3, or higher dimensional cell arrays. Here’s an example of a 2D cell array: c{1,1}=1; c{1,2}=’bye’; c{2,1}=1:10; c{2,2}=[1 2 3;4 5 6;7 7 7]; Cell arrays are useful for organizing data and for use in functions. ----------------------------------------------------------------------------------------- Functions There are two types of m-files: 1) Scripts (the types of programs you’ve been writing so far) 2) Functions: a) Matlab has many functions that you’ve seen, for example, mean(x) or std(x) or max(x) to which you provide inputs (an array or matrix or some argument), and it returns a value such as mx=mean(x) where mx is the returned value and x is the argument fed into the function b) You can write your own functions in Matlab c) Functions also have extension .m d) Any variable defined in the body of a function is a LOCAL variable: 2 invisible to any script program that calls the function invisible to the command window x in a function is not the same as an x in the command window When a function is called, a temporary memory space is created that is separate from the global memory. Once the function is finished running, that temporary space and all that it contains will be deleted. Best shown by an EXAMPLE: write a function that calculates mean, std, min, max, and median of an array and prints it to the command window. Then plots the array. Lets pick a name for the function, for example, allstats First line of ANY function program must contain: 1) the word function, followed in brackets. Inside the brackets you must list any variables that your function will output. function [mx,sx,minx,maxx,medx] 2) This is followed by equal sign and the name of the function (e.g., allstats). This name, allstats, must be the same as the name of the ‘m file’ itself. function [mx,sx,minx,maxx,medx] = allstats 3) This is followed by input variables (varin1, but you could call it x or anything else) function [mx,sx,minx,maxx,medx] = allstats (varin1) Important: First line of a function cannot contain a % remark, it must contain the word function as described above. Summary of first line: 1) The word “Function” 2) Output variables in brackets 3) Function name 4) Input variables in parentheses Body of Function: Calculate all the variables that we want to output to the command window mx=mean(varin1); sx=std(varin1); minx=min(varin1); maxx=max(varin1); medx=median(varin1); plot(varin1); 5 Subfunctions: 1) created in the body of a function and at its end 2) created by using the keyword “function” 3) its variables are not visible to the main body of the function 4) its output is visible to the main body of the function 5) not visible outside the main function (i.e., command window) 6) can have multiple subfunctions lined up at the end of the main body 7) example (previous progrogram): function [mx,my,sx,sy] = allstats(varin1,varin2) %This function calculates mean and std of two different %arrays and plots both. It has four outputs, the two means and %the two sigmas in order mx=mean(varin1); %or mx=mean(x) if that is what you called it sx=std(varin1); my=mean(varin2); sy=std(varin2); plot(varin1); hold on; plot(varin2,'r'); hold off; Lets say, that we want a new statistic, called mymodel whish is: m=sqrt(prod(xi)*sum(xi)/sigma(xi)); m=m1*exp(2); m=log10(m); Instead of typing this once for each variable, you could create a subfunction, called mymodel with input being varin1 or varin2. %-----------------------------------subfunction 1 function m=mymodel(w) m=sqrt(prod(w)*sum(w)/std(w)); m=m*exp(2); m=log10(m); We can rewrite the above function as: function [mx,my,sx,sy,m1,m2] = allstats3(varin1,varin2) %This function calculates mean and std of two different %arrays and plots both. It has four outputs, the two means and %the two stds in order mx=mean(varin1); %or mx=mean(x) if that is what you called it 6 sx=std(varin1); m1=mymodel(varin1); my=mean(varin2); sy=std(varin2); m2=mymodel(varin2); plot(varin1); hold on; plot(varin2,'r'); hold off; %-----------------------------------subfunction 1 function m=mymodel(w) m=sqrt(prod(w)*sum(w)/std(w)); m=m*exp(2); m=log10(m); Note on visibility: m in the subfunction will not be visible to the body of the function, so if you type disp(num2str(m)) immediately after hold off, the function will return an error message: undefined variable m ----------------------------------------------------------------------------------------------------- Special variables reserved for functions nargin, nargout, varargin, varargout These four commands can only be used INSIDE a function (they are invisible to the base workspace). The first two commands (nargin and nargout) provide information on what the user is doing: nargin tells you how many variables are supplied to a function when the user runs the function in the command window or in a program. nargout tells you how many variables are requested from a function when the user runs the function in the command window or in a program varargin allows the user to specify a variable number of input argument to a function. It is used only in the function itself and is invisible to the base workspace. varargin is a cell array. 7 varargout allows the user to specify a variable number of output arguments from a function. It is used only in the function itself and is invisible to the base workspace. varargout is a cell array. -------------------------------------------------------------------------------------------------------- Problem set 3: Graphics/Functions (Due Wednesday Nov. 7). 1) Create a cell array that contains 3 rows and two columns. In the three cells of the first column enter three separate numbers (scalars) In the first row of the second column enter a string variables In the second row of the second column enter an array of 5 numbers, e.g., [2 3 5 - 1 0] In the third row of the second column enter a 3 by 4 matrix of random numbers 2) Write a function named ‘randun’ that returns two rxc arrays of random numbers. The first array should contain random numbers picked from a uniform distribution, and the second array should contain random numbers from a normal distribution. Example: [x , y]=randun(3,2) should return a matrix called x that has 3 rows and 2 columns with each entry picked from a uniform distribution, and a matrix called y with 3 rows and 2 columns with entries picked from a normal distribution. 3) Write a function that plots four different types of 3D graphs as four panels in the same figure window (e.g., mesh, meshc, meshz, and contour). The function should accept as input arguments: 1) an mxn matrix of numbers, and 2) a string variable that is used as an option to set the colormap.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved