%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATLAB INTRO TUTORIAL % BY CNSO % SEPT 23, 2009 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % I suggest copying examples line-by-line into the command window and % running them from there - that way you can see the results of each % command separately % Most importantly, play with all the examples: run them and look at the % results, change something, run them again and see what happens! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % VARIABLES % numeric variable a=12.7 variables_names_can_be_weird=0.345 % compare to a=12.7; % output is suppressed % text (character,string) variable st='This is simple!' % logical (boolean) variable bl=true bl=(a==3) bl=(a>=5) % numeric array b=[1,2,3] % compare to b=[1 2 3] c=[4;5;6] d=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16] e=[1:5;1:2:9] % access to array elements d d(3,4) d(1:4,2) % compare to d(1:end,2) % and to d(:,2) % advanced example e(1:2,1:2)=d([1,3],[2,4]) [d(:,3),d;d(2,:),d(end,end)] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATH OPERATIONS b' 3*b b+c' b*c c*b b.*c' % advanced example f=(d(1:3,1:3)+c*b)/10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % FUNCTIONS sqrt(a) sum(d) sum(d,2) zeros(5,5) 3*ones(3,4) % advanced example g=find(abs(d-8*ones(4,4))>6) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % FLOW CONTROL TOOLS % 'if' - executing commands if condition is true h=5; if (h>3) disp('true'); else disp('false'); end % 'for' - executing commands fixed number of times for i=1:10 disp('new iteration'); i end % 'while' - repeatedly executing commands while the condition is true j=7; while (j>0) disp('new iteration'); j j=j-1; end % advanced example k=zeros(5,5); for i=1:5 for j=1:5 k(i,j)=3; end end % often there is a way to avoid loops and it is almost always faster k=3*ones(5,5); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PLOTTING x=0:0.05:2*pi; y=sin(x); plot(x,y); % advanced example figure; plot(x,y,x,(x-pi)/2,'LineWidth',3); xlim([0,2*pi]); xlabel('x axis'); ylabel('y axis'); legend('sinusoid','straight line'); grid('on'); title('title'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % SAVING AND LOADING DATA save('my_data.mat','a','b','c','d'); clear('all') load('my_data.mat'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % GETTING HELP help sqrt help find % use MATLAB "Product help" via the upper menu in the MATLAB main window % (shortkey F1) to do the "Getting Started" tutorial, play with "Demos", or % search for functions you need % for example, search for "using the plot catalog" - a useful tool to get % you started with different visualization tools % you can always google specific MATLAB questions - there are a lot of % specialized forums on the internet % HAVE FUN WITH MATLAB!