Besides numbers, another data type in matlab is the character data type. Characters are designated by enclosing them in single quotes. A string is a horizontal 1D character array, whose length is the number of characters.
Besides numbers, another data type in matlab is the character data type. Characters are designated by enclosing them in single quotes. A string is a horizontal 1D character array, whose length is the number of characters.
Besides numbers, another data type in matlab is the character data type. Characters are designated by enclosing them in single quotes. A string is a horizontal 1D character array, whose length is the number of characters.
Besides numbers, another data type in Matlab is the
character data type. Characters are designated by enclosing them in single quotes. >> C = x C = x >> x1=double(c) x1 = 120 >> char(65) ans = A Strings A sequence of characters within single quotes is referred to as a string. >> D = Training' ; A string is a horizontal 1D character array, whose length is the number of characters. The string D above has eight characters, and is therefore a 1-by-8 array. >> size(D) ans = 1 8 Individual elements can be referenced as for any other array, such as D(5) which is element n'. Cell arrays Array for which the elements are cells and can hold other MATLAB arrays of different types. to point to elements of cell arrayis the use of curly braces {...}. Using celldisp function to display cell array A(1,1) = {[1 4 3; 0 5 8; 7 2 9]}; A(1,2) = {'Anne Smith'}; A(2,1) = {3+7i}; A(2,2) = {-pi:pi/10:pi}; Structures Arrays with named data containers called fields. patient.name='John Doe'; patient.billing = 800.00; patient.Ptest= [79 75 80;110 108 120]; Also, Build structure arrays using the struct function. patient(1) =struct('name', 'John Doe', billing',800,ptest', [79 75 80;110 108 120]) Multidimensional Arrays The first references array dimension 1, the row. The second references dimension 2, the column. The third references dimension 3, The page. A = zeros(3,3,4); A(:,:,2) = ones(3) A(:,:,1) = 0 0 0 0 0 0 0 0 0 A(:,:,2) = 1 1 1 1 1 1 1 1 1 A(:,:,3) = 0 0 0 0 0 0 0 0 0 A(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Page N Page 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20 Script and Function Files Script Files Work as though you typed commands into MATLAB prompt Variable are stored in MATLAB workspace Function Files Let you make your own MATLAB Functions All variables within a function are local All information must be passed to functions as parameters Subfunctions are supported II-Scripts and Functions Script-files contain a sequence of Matlab commands create a file called test1.m that contains these MATLAB commands %this program is to help me understand the scripts im matlab x=1:0.1:15; y=10*sin(x)-7*log10(x.^2); plot(x,y); grid on Executed by typing its name >> test1 Scripts and Functions (contd) Displaying code and getting help To open and edit the code , use edit command >> edit test1 To list code, use type command >> type test1 The help command displays first consecutive comment lines >> help test1 Functions Functions describe subprograms Take inputs, generate outputs Have local variables (invisible in global workspace) The names of the M-file and of the function should be the same. Comments may be placed above the function header Basic Parts of a Function M-File function y = mean (x) % MEAN Average or mean value. % For vectors, MEAN(x) returns the mean value. % For matrices, MEAN(x) is a row vector % containing the mean value of each column. [m,n] = size(x); if m == 1 m = n; end y = sum(x)/m; Output Arguments Input Arguments Function Name Online Help Function Code 47 Function Example Evaluate the function f(x,y) [a b] = myfun(10,20); =============================== where the M-file myfun.m is Function [w,z] = myfun(x, y) z = y*sin(x)+x*cos(y); W=x+y Vectorization Loops are slow: Replace loops by vector operations! Memory allocation takes a lot of time: Pre-allocate memory! Use profileto find code bottlenecks! Example tic x=-250:0.1:250; for ii=1:length(x) if x(ii)>=0, s(ii)=sqrt(x(ii)); else s(ii)=0; end; end; tic x=-250:0.1:250; s=sqrt(x); s(x<0)=0; toc; Preallocation This code uses the function ones to preallocate the vectors (r1,r2) created in the for loop.This makes the for loop execute significantly faster. r1 =ones(1,100); r2=ones(1,100); for n =1:100; x=rand(1,n); r1(n) =sum(x); r2(n) =mean(x); end Function Functions That is, one function works on another function. The function functions include Zero finding Optimization Quadrature Ordinary differential equations ==================================================================== Create an m file for the function test. functiony=test(x); y=7*sin(x)-7*log10(x.^2) ======================================== Evaluate this test function at a set of points in the interval 1 <x <20 w =1:0.1:20; z =test(w); Function Functions(contd) Then plot the function with Plot(w,z); The output will be Function Functions(contd) Find the minimum of the function using(fminsearch) min =fminsearch(@test,17) min = 17.3289 ================================================= To evaluate the function at the minimium value, test(min) ans = -24.3340 rough guess at the location of the minimum. To compute the area under the curve in the graph int =quadl(@test,0,1) int = 9.2980 if you search for a zero z =fzero(@test,2) you will find one outside the interval z = 2.3212 Function Functions(contd) Thank You