1 / 18

Functions

Learn about scripts, workspace, global variables, passing data to scripts, and functions in MATLAB. Includes examples and explanations.

mbelanger
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions

  2. Scripts and Workspace • scripts are just a collection of commands, running a script is same as running the statements in command window. • Your scripts and command window shares the same set of variables, also called the workspace.

  3. Passing a variable to a script script.m : disp(num2str(x)) x = 5; Command Window: >>x=2; >>script 2 >>x x = 5

  4. Global Variables • Variables in scripts and command window are shared, in other words, they are global. • Global variables have been found to be problematic, since your program could erase a value needed by some other script, or the values you depend on might be changed by some other program. These side-effects are difficult to find • In large systems, name clashes would be very common.

  5. Passing Data to a Script • Let’s say we have a script that sums the values in a vector : sum_vect.m : sum = 0; for x=1:length(v) sum = sum + v(x) end

  6. Passing Data to a Script • We have another program where we are given three vectors: a,b, & c. We want to take the average of all elements in them. How can we make use of sum script? v = a; sum_vect, suma = sum; v = b; sum_vect, sumb = sum; v = c; sum_vect, sumc = sum; average = (suma+sumb+sumc)/(length(a) + length(b)+length(c))

  7. A better way? • Take the rem function • Do we call it like: >>numerator=7,denominator = 3; rem; disp(num2str(result)) • We do: >>disp(num2str(rem(7,3))); • We just send our data as parameters, and the expression will be evaluated to return value of our function

  8. Horizontal Line Example • Rather than getting the input from user every time, we can write our own function horizontalLine: function horizontalLine(a, b, y, g) for x=a:b-1 putPixel(x, y, g); end >>>> horizontalLine(10, 300, 20, g)

  9. Drawing a Rectangle function drawRectangle(x1, y1, x2, y2, g) x = y1; while (x <= y2) horizontalLine(x1, x2, x, g) x = x + 1; end what about x? horizontalLine also have a variable x !

  10. Functions have private workspaces >> whos Name Size Bytes Class fr 1x1 javax.swing.JFrame g 1x1 sun.awt.windows.WGraphics mines 400x400 160000 logical array Grand total is 160002 elements using 160000 bytes >> horizontalLine(10, 200, 20, g) >> x ??? Undefined function or variable 'x'.

  11. Functions as Black Boxes • Functions are machines that get some input and produce some output. • We cannot see, or shouldn’t care to see the inner workings of a function, the local variables used by the function and so on. • functions cannot see or use variables that exist in the context of the caller. • when you pass input arguments to functions, you are not passing variables, just simple values.

  12. Function calls a=10 the only information that goes from a function to another is the values of parameters. The callee cannot see or know which variables the caller has function x1(a1) a1 =20; x2(a1) function x a = 10; x1(a) a1=20 function x2(a1) a1 =30; x3(a1 + 10) a1=30 function x3(a3) a1 =20; a3=40 a1=20

  13. A closer look at putPixel function suite • initWindow : fr = javax.swing.JFrame('Drawing Pad'); fr.setSize(400, 400); mines = rand(400, 400) > 0.99; fr.show; g = fr.getContentPane.getGraphics; • it creates global variables mines and g

  14. PutPixel function function putPixel(x, y, g) g.drawLine(x,y, x,y) • Since putPixel cannot see the global variable g, we have to pass it ourselves • Note that we could also write: function putPixel(x, y, graphicsObject) • You can give any variable name you want

  15. Functions that Return values function minValue = minVector(v) minValue = v(1); for i = 2:length(v) if (v(i) < minValue) minValue = v(i); end end >> v = [5 3 7 1 9 11] v = 5 3 7 1 9 11 >> minVector(v) ans = 1

  16. Function that Returns multiple values function [minValue, minIndex] = minVector(v) minValue = v(1); minIndex = 1; for i = 2:length(v) if (v(i) < minValue) minValue = v(i); minIndex = i; end end >> minVector(v) ans = 1 >> [a b] = minVector(v) a = 1 b = 4

  17. Complete Form function [minValue, minIndex] = minVector(v) %MINVECTOR function that returns minimum of a vector % it returns two values, one the minimum % value in the vector, the other, the index of the minimum value minValue = v(1); minIndex = 1; for i = 2:length(v) if (v(i) < minValue) minValue = v(i); minIndex = i; end end

  18. help and lookfor >> lookfor minVector MINVECTOR function that returns minimum of a vector >> >> help minVector MINVECTOR function that returns minimum of a vector it returns two values, one the minimum value in the vector, the other, the index of the minimum value

More Related