MATLAB_INTRO - A Hands on Introduc.on to MATLAB Eric Harley

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: A Hands on Introduc.on to MATLAB Eric Harley eric.harley@jhu.edu JHU Intersession 2010 Intro. to MATLAB || Eric Harley JHU Intersession 2010 1 Push BuDon Axis Label Pop-up Menu Intro. to MATLAB || Eric Harley JHU Intersession 2010 2 Creating GUIs with MATLAB GUIs in MATLAB come in two parts .fig file contains complete descrip.on of GUI components and the graphical layout .m file contains event handling code GUIDE is MATLAB's GUI Design Environment Easy to drag/drop GUI elements onto a figure Rapidly prototype, interact with GUIs 3 Creating GUIs with MATLAB Stages of GUI Crea.on 1. Designing and laying out the GUI 2. Programming the event handlers (callbacks) 3. Running the GUI Start GUIDE with >> guide 4 The Layout Editor Component PaleDe Run buDon Property inspector M-file Editor Layout Area Figure Resize 5 Callbacks / Event Handlers A callback is a sequence of commands that are execute when a graphics object is ac.vated Stored in the GUI's M-file A property of a graphics object e.g. CreateFnc, BuDonDwnFnc, Callback, DeleteFnc % --- Executes on button press in button1.! function button1_Callback(hObject,eventdata, handles)! % hObject handle to pushbutton1! % eventdata reserved! % handles structure with handles and user data! 6 Callbacks / Event Handlers A callback is usually made of the following stages: 1. Get the handle of the object triggering the ac.on The object provides informa.on / values about the event 2. Gecng the handles of the objects being affected The objects whose proper.es are to be changed. 3. Gecng necessary informa.on / values 4. Doing some processing and calcula.ons. 5. Secng relevant object proper.es to update display. 7 A Simple GUI Design and Build a GUI to: add together two numbers, display the result in a text field. 8 A Simple GUI: Design 1. Start GUIDE, open a Blank GUI 2. Add components: 1. Two Edit Text 2. Four Sta.c Text 3. One PushbuDon 9 A Simple GUI: Design 3. Double-click one of the Sta.c Text components to open Property Inspector 4. String property is the text that is displayed by the object. Change this to +, and increase font size to 20 from 8 10 A Simple GUI: Design 5. Do the same for = 6. Create a .tle string, like "My Adder GUI," give it a color, increase the font size. 11 A Simple GUI: Design 7. For the last sta.c text control, set the string to 0 8. Change the Tag to answer_staticText Tag parameter is essen.ally the variable's name 12 A Simple GUI: Design At this point... 13 A Simple GUI: Design 9. Modify the Edit Text control. Name one input1_editText input2_editText 10. Ini.alize text to 0 14 A Simple GUI: Design 11.Modify the push buDon control. Name it add_pushButton 15 A Simple GUI: Design Reposi.on components to look something like this Save as myAdder 16 A Simple GUI: Code MATLAB automa.cally generates an .m file to go along with the The .m file is where we aDach the appropriate code to the callback of each component. Click on "" which will bring up a list of the func.ons within the .m file. Select input1_editText_Callback. 17 A Simple GUI: Code Boiler plate: % --- Executes on button press in button1. function input1_editText_Callback(hObject,eventdata, handles) % hObject handle to pushbutton1 % eventdata reserved % handles structure with handles and user data % Hint: get(hObject,'String') returns contents of % input1_editText as text % str2double(get(hObject,'String')) returns contents % of input1_editText as a double 18 A Simple GUI: Code % --- Executes on button press in button1. function input1_editText_Callback(hObject,eventdata, handles) % store the contents of input1_editText as a string. % if the string is not a number then input will be empty input = str2num(get(hObject,'String')); % checks to see if input is empty. if so, default % input1_editText to zero Makes sure that the input is if (isempty(input)) well defined. set(hObject,'String','0') end Tells the GUI to update the guidata(hObject, handles); handles structure aper the callback is complete 19 Add the same block of code to input2_editText_Callback. A Simple GUI: Code % --- Executes on button press in add_pushbutton.! function add_pushbutton_Callback(hObject, eventdata, ... ! a = get(handles.input1_editText,'String');! b = get(handles.input2_editText,'String');! % a and b are variables of Strings type, need conversion! % to numbers before they can be added together! total = str2num(a) + str2num(b);! c = num2str(total);! % need to convert back into string type to display it! set(handles.answer_staticText,'String',c);! guidata(hObject, handles);! 20 Structures Structures are a datatype that hold data in named fields s is a 1x2 matrix of structures A Simple GUI: Code % --- Executes on button press in add_pushbutton.! function add_pushbutton_Callback(hObject, eventdata, ... ! a = get(handles.input1_editText,'String');! b = get(handles.input2_editText,'String');! % a and b are variables of Strings type, need conversion! % to numbers before they can be added together! total = str2num(a) + str2num(b);! c = num2str(total);! % need to convert back into string type to display it! set(handles.answer_staticText,'String',c);! guidata(hObject, handles);! 22 A Simple GUI 23 Another GUI: Plots The Axes component displays graphics like graphs and images 24 Another GUI: Plots Tags plotAxes1_pushbuDon plotAxes2_pushbuDon clearAxes_pushbuDon 25 Axes GUI Code plot1_pushbutton_Callback %selects axes1 as the current axes, so that %Matlab knows where to plot the data axes(handles.axes1) %creates a vector from 0 to 10, [0 1 2 3 . . . 10] x = 0:10; %creates a vector from 0 to 10, [0 1 2 3 . . . 10] y = 0:10; %plots the x and y data plot(x,y); %adds a title, x-axis description, and y-axis description title('Axes 1'); xlabel('X data'); ylabel('Y data'); guidata(hObject, handles); %updates the handles 26 Axes GUI Code clearPlots_pushbutton_Callback %these two lines of code clears both axes cla(handles.axes1,'reset') cla(handles.axes2,'reset') guidata(hObject, handles); %updates the handles axesGUI_OpeningFcn %add plot toolbar to GUI set(hObject,'toolbar','figure'); guidata(hObject, handles); %updates the handles 27 Fourier Transforms are used in X-ray diffrac.on Electron microscopy (and diffrac.on) NMR spectroscopy IR spectroscopy Fluorescence spectroscopy Image processing etc. Fast Fourier Transform The Fast Fourier Transform (FFT) is a very efficient algorithm for performing a discrete Fourier transform FFT principle first used by Gauss in 1805 FFT algorithm published by Cooley & Tukey in 1965 In 1969, the 2048 point analysis of a seismic trace took 13 hours. Using the FFT, the same task on the same machine took 2.4 seconds! Fourier Analysis fo = 4; ! !%frequency of the sine wave! Fs = 100; ! !%sampling rate! Ts = 1/Fs; ! !%sampling time interval! t = 0:Ts:1-Ts; !%sampling period! n = length(t); !%number of samples! y = 2*sin(2*pi*fo*t); !%the sine curve! %plot the curve in the time domain! figure;! plot(t,y)! xlabel('time (seconds)')! ylabel('y(t)')! grid! Intro. to MATLAB || Eric Harley JHU Intersession 2010 30 Fourier Analysis %plot the frequency spectrum using the fft command! figure;! YfreqDomain = fft(y); !%take the fft of our sin wave, y(t)! stem(abs(YfreqDomain)); !%abs gets the magnitude! !! ! ! ! ! ! !%angle would get the phase plot!! xlabel('Sample Number')! ylabel('Amplitude')! title('Using the Matlab fft command')! grid! axis([0,100,0,120])! Intro. to MATLAB || Eric Harley JHU Intersession 2010 31 Fourier Analysis [YfreqDomain,frequencyRange] = centeredFFT(y,Fs);! figure;! %remember to take the abs to get the magnitude!! stem(frequencyRange,abs(YfreqDomain));! xlabel('Freq (Hz)')! ylabel('Amplitude')! title('Using the centeredFFT function')! grid! axis([-6,6,0,1.5])! Intro. to MATLAB || Eric Harley JHU Intersession 2010 32 Touch Tone Dialing 1209 Hz + 697 Hz make the 1 tone Intro. to MATLAB || Eric Harley JHU Intersession 2010 33 Touch Tone Dialing fr = [697 770 852 941];! fc = [1209 1336 1477];! s = '1';! switch s! case '*', k = 4; j = 1;! case '0', k = 4; j = 2;! case '#', k = 4; j = 3;! otherwise,! d = s-'0';! j = mod(d-1,3)+1;! k = (d-j)/3+1;! end! Fs = 32768;! t = 0:1/Fs:0.25;! y1 = sin(2*pi*fr(k)*t);! y2 = sin(2*pi*fc(j)*t);! y = (y1 + y2)/2; ! [Y,freq] = centeredFFT(y,Fs);! figure;! stem(freq,abs(Y));! xlabel('Freq (Hz)')! ylabel('Amplitude')! title('Using the centeredFFT function')! grid! axis([500 1700 0 1])! Intro. to MATLAB || Eric Harley JHU Intersession 2010 34 Touch Tone Dialing Can you make this? Key Pressed 5 Intro. to MATLAB || Eric Harley JHU Intersession 2010 35 ...
View Full Document

This note was uploaded on 05/04/2010 for the course NUMERICAL ID101 taught by Professor Shnur during the Spring '10 term at 카이스트, 한국과학기술원.

Ask a homework question - tutors are online