New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here


by: Dr. Adriana Bernier


Dr. Adriana Bernier
GPA 3.78


Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Class Notes
25 ?




Popular in Course

Popular in Cognitive Sciences

This 6 page Class Notes was uploaded by Dr. Adriana Bernier on Saturday September 12, 2015. The Class Notes belongs to Psych 205 at University of California - Irvine taught by Staff in Fall. Since its upload, it has received 42 views. For similar materials see /class/201843/psych-205-university-of-california-irvine in Cognitive Sciences at University of California - Irvine.

Similar to Psych 205 at UCI

Popular in Cognitive Sciences




Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/12/15
Cell arrays Cells are a form of data structure that allow different types of data types to be saved in the same variable ie numbers vectors strings Braces or curly brackets are used instead of or to designate a variable as a cell array Example cl1 number or scalar c2 hello string c3110 vector will create a 3cell array that contains a number a string and an array as its entries When calling back cell arrays use curly brackets C1 C2 C3 Could also do create 2 3 or higher dimensional cell arrays Here s an example of a 2D cell array c1l1 c12 bye c21110 c2212 34 5 67 7 7 Cell arrays are useful for organizing data and for use in functions Fun ctions There are two types of mfiles 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 meanx or stdx or maxx to which you provide inputs an array or matrix or some argument and it returns a value such as mxmeanx 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 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 mXSXminXmaXXmedX 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 ofthe m file itself function mXSXminXmaXXmedX allstats 3 This is followed by input variables varinl but you could call it X or anything else function mXSXminXmaXXmedX allstats varinl 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 mXmeanvarinl SXstdvarinl minXminvarin l maXXmaXvarinl medXmedianvarinl plotvarinl Results output of a function Note on output If you don t specify which output you want when you run the function it will give you only the first output variable in this case mX To get all of them you must specify all output variables when running the function abcdeallstatsX abcdallstatsX will only print the first 4 excluding median acdallstatsX can t do this Program will crash abcdefallstatsX will give an error message too many output arguments Note on comments and help After the first line you can add comments to the body of function or any m file The first comment line that is on a line by itself and all consecutive lines of comments following it may be used to describe what the function does You can then use help allstats or whatever the name of your function is to get the description specified by the comments line help allstats will not print any comment which is on the same line as a command ie mXmeanvarinl mean of input var Note on two or more input variables same as for one var For example if X and y are two variables defined in the command window function mXmysxsy a11statsvarinlvarin2 This function calculates mean and std of two different arrays and plots both It has four outputs the two means and the two standard deviations mXmeanvarinl sxstdvarinl myme anvarin2 systdvarin2 plotvarinl hold on plotvarin2 r hold off In the command window type abcdallstats2Xy to run this function Global vs Local variables Normally variables in a function are local and not recognized by the base workspace Variables in the base workspace are not recognized in the body of a function ie the local workspace If several functions and an mf11e script or the base workspace all declare a particular variable say X as global then they all share a single copy of that variable However if functionl and base workspace declare X as global but function2 does not then the X in function 2 is completely separate from the X in functionl and base workspace You must declare the variables as global BEFORE you de ne them Example In base workspace type global X y X000110 ySinX Write a function called plotl that plots any X and y value function plotl note input and output not required for a function global X y p10txy In the command window type plotl to run the function You will note that the function did recognize X and y since they were declared global Do not use a comma between the variables you are declaring as global If you type global Xy it will change X to global and then just print y as a separate command won t make y global Make sure there are no commas between variables when being declared as global The word global must be all lowercase You need to declare a variable global in all functions and base workspace if you want it to be visible to these functions AND base workspace If a variable is just declared global in a single function it will not be visible to any other functions or base workspace It must be declared global wherever you d like it to be shared The reason for this is that many function including those that are standard Matlab functions such as mean or std may use a copy of the variable you intend to make global say X for other purposes and that Matlabdefined function may get confused if X became global everywhere To clear a global variable X type clear global X Subfunctions 1 lCNUIJkUJN VVVVVV created in the body of a function and at its end created by using the keyword function its variables are not visible to the main body of the function its output is visible to the main body of the function not visible outside the main function ie command window can have multiple subfunctions lined up at the end of the main body example previous progrogram function mXmysxsy allstatsvarinlvarin2 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 mXmeanvarinl or mXmeanX if that is what you called it sxstdvarinl mymeanvarin2 systdvarin2 plotvarinl hold on plotvarin2 r hold off Lets say that we want a new statistic called mymodel whish is msqrtprodXisumXisigmaXi mmleXp2 mlog10m Instead of typing this once for each variable you could create a subfunction called mymodel with input being varinl or varin2 0 I L c 1 function mmymodelw msqrtprodw sumwstdw mmeXp2 mlog10m We can rewrite the above function as function mXmysxsym1m2 allstats3varinlvarin2 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 mXmeanvarinl or mXmeanX if that is what you called it sxstdvarinl m1mym0delvarin1 mymeanvarin2 systdvarin2 m2mym0delvarin2 plotvarinl hold on plotvarin2 r hold off 0 I L c 1 function mmymodelw msqrtprodwsumwstdw mmeXp2 mlog10m Note on visibility m in the subfunction will not be visible to the body of the function so if you type dispnum2strm 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 rst 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 varargz39n is a cell array


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Anthony Lee UC Santa Barbara

"I bought an awesome study guide, which helped me get an A in my Math 34B class this quarter!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.