《关于Matlab中的GUI的Plotting-Data-to-Axes精品资料.doc》由会员分享,可在线阅读,更多相关《关于Matlab中的GUI的Plotting-Data-to-Axes精品资料.doc(104页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、MATLAB GUI Tutorial - Plotting Data to Axes 31 Oct 2007 Quan Quach 175 comments 25,632 views IntroductionIn this Matlab GUI tutorial, you will learn how to create and use the Axes component. The Axes component allows you to display graphics, such as graphs and images on your GUI. In this tutorial, w
2、e will create two axes on the GUI and plot some simple data onto it. In addition, we will include a reset button to clear the axes and we will also add the standard toolbar to allow the user to zoom, pan, and query the plot. This tutorial is written for those with little or no experience creating a
3、Matlab GUI (Graphical User Interface). If youre new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab is recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it is
4、ant too outdated). Lets get started!Create the Visual Aspect of the GUI1. First, open up Matlab. Go to the command window and type in guide.2. You should see the following screen appear. Choose the first option Blank GUI (Default).3. Click on and add two Axes components to the GUI figure. Next, clic
5、k on and add three Pushbutton components onto the GUI figure. 4. Double click the Axes component to bring up the Property Inspector. Change the Tag property to axes1, which should already be the default name. Additionally, make sure the other Axes components Tag property is named axes2. 5. Next, let
6、s modify the properties of the Pushbutton components. Double click on one of the Pushbutton components. Change the String property to Plot Axes 1, and the Tag property to plotAxes1_pushbutton, as shown below.Similarly, double click on the next pushbutton and change the String property to Plot Axes 2
7、 and change the Tag property to plotAxes2_pushbutton. Finally, double click on the final pushbutton and change the String property to Clear Axes and change the Tag property to clearAxes_pushbutton. 6. Heres what your figure should look like after you add the components and modify them.7. Save your G
8、UI wherever you please with your desired filename. Writing the Code for the GUIMatlab automatically generates an .m file to go along with the figure that you just put together. The .m file is where we attach the appropriate code to the callback of each component. For the purposes of this tutorial, w
9、e are primarily concerned only with the callback functions. You dont have to worry about any of the other function types.1. Open up the .m file that was automatically generated when you saved your GUI. In the Matlab editor, click on the icon, which will bring up a list of the functions within the .m
10、 file. Select plot1_pushbutton_Callback. Add the following code to the function:%selects axes1 as the current axes, so that %Matlab knows where to plot the dataaxes(handles.axes1)%creates a vector from 0 to 10, 0 1 2 3 . . . 10x = 0:10;%creates a vector from 0 to 10, 0 1 2 3 . . . 10y = 0:10;%plots
11、the x and y dataplot(x,y);%adds a title, x-axis description, and y-axis descriptiontitle(Axes 1);xlabel(X data);ylabel(Y data);guidata(hObject, handles); %updates the handles2. Similarly, we want to put the following code into the plot2_pushbutton_Callback:3. %selects axes2 as the current axes, so t
12、hat 4. %Matlab knows where to plot the data5. axes(handles.axes2)6. 7. %creates a vector from 0 to 10, 0 1 2 3 . . . 108. x = 0:10;9. %creates a vector 0 1 4 9 . . . 10010. y = x.211. 12. %plots the x and y data13. plot(x,y);14. %adds a title, x-axis description, and y-axis description15. title(Axes
13、 2);16. xlabel(X data);17. ylabel(Y data);guidata(hObject, handles); %updates the handles18. Next, we need to add some code to the clearPlots_pushbutton_Callback:19. %these two lines of code clears both axes20. cla(handles.axes1,reset)21. cla(handles.axes2,reset)guidata(hObject, handles); %updates t
14、he handles22. And finally, we need to add the following line of code to axes_tutorial_OpeningFcn:set(hObject,toolbar,figure);This line of code should be placed right before:guidata(hObject, handles);This line of code effectively adds the standard toolbar to the GUI, allowing the user to zoom, pan, q
15、uery the plot, and more. The standard toolbar and a brief description of the icons are shown below:23. Save your m-file!Run and Test the GUINow that weve completed both the visual and code aspects of the GUI, its time to run the GUI to make sure it works.1. From the m-file editor, you can click on t
16、he icon to save and run the GUI. Alternatively, from the GUIDE editor, you can click on the to launch the GUI. The following GUI should appear once you click the icon: 2. Go ahead and try pressing all of the buttons to make sure they work. If everything was done correctly, you should see the followi
17、ng plots. Also, you can use the icons that are within the red box to test out the other functions. 3. And thats it. Those are the basics of using the Axes component. You can explore the other options that the axes has to offer through the Property Inspector. This is the end of the tutorial. Source f
18、iles can be downloaded here.175 Responses to “MATLAB GUI Tutorial - Plotting Data to Axes”1. on 12 Jan 2008 at 12:48 pm 1Vaibhav BediaWhen i close MATLAB and start guide again my axes loses its TAG and hence becomes invisible.How do i solve this?2. on 20 Jan 2008 at 4:48 pm 2Alexnice one! U need to
19、write some more stuff for beginners 3. on 13 Feb 2008 at 3:59 pm 3SaikatThank you so much . I love your style and find your instructions very helpful.Going through the programs you have written for us, the beginners, I feel more confident of writing my own codes .Thank you once again .4. on 17 Feb 2
20、008 at 11:03 am 4TantyWhat a great tutorial.Im working on my final project, so this tutorial help me solve my problem writing the code for axes.Thanks.5. on 17 Mar 2008 at 9:57 pm 5lovejoyhow to plot a resultant vector in GUI ex.10N+5N6. on 26 Mar 2008 at 4:28 am 6nolavery nice tutorial.however i ha
21、d a query for you.when i do a zoom on the image.and click one pushbutton to make an action.the image return to its initial dimensions(i loose the zoom)could you help me on this subject?7. on 26 Mar 2008 at 12:37 pm 7Daniel SutoyoHi Nolagood question although I havent tried to code it yet, what you n
22、eed to do is to get the new axes info and store it somewhere.For example, your axes is called handles.axes1. You will have to use the get() and get the min,max axis on handles.axes1 and store in a variable for example myaxis.Now in your pushbutton function when you have a plot command, make sure you
23、 use the axis info in myaxis8. on 10 Apr 2008 at 3:43 am 8andyHow do we plot a graph where the function of graph is key in by user?I mean we can plot many different function of graph just by changing function?9. on 17 Apr 2008 at 11:42 am 9harshavery nice, it helped me a lot. Thanks, I love the way
24、u have explained the whole process. very clear! 10. on 22 Apr 2008 at 9:57 am 10Alfredo Huerta DuranMuy buena tu pagina, la estare consultando a menudo. De antemano te doy las gracias por la futuras visitas a tu tutorial. Alfredo Huerta11. on 13 Jun 2008 at 11:58 pm 11shanshui281Thank you very much
25、!I learna lot of skills from this websit!12. on 22 Jun 2008 at 10:56 pm 12abhijit betalDear Sir,This tutorial helps me a lot. Thanks for it. Will you provide a tutorial on basic concept of the GUIDE ?With thanksAbhijit Betal13. on 23 Jun 2008 at 12:40 pm 13sathyaI used a series of checkboxes, each t
26、o plot the derivatives of a function.When I click on the checkbox, the plot sholud appear and when I unclick it, the plot should disappear. I used hold on on each checkbox callboxes, so that when I click all of them I should see all the plots. I tried using cla, but it clears the whole axes.If I unc
27、heck the first checkbox, only the plot corresponding to it should disappear.Any help?14. on 01 Jul 2008 at 3:27 am 14ahmed badrcan i ask questioni have plot between eficiency and threshold and i get max efficiencycan i get threshold on max efficiency in gui of matlab code15. on 22 Jul 2008 at 2:55 p
28、m 15ArashDear sir,I want to know how to capture the coordination of my graph?can you help me with that16. on 22 Jul 2008 at 5:01 pm 16Quan QuachArash,Can you elaborate on your question? I dont quite understand it.Quan17. on 23 Jul 2008 at 11:46 am 17ArashQuan,I want know is it any way to capture (x,
29、y) of graph after plotting it,I mean after plotting a graph, I want to be able to click on graph and see dots on graph at my every (x,y), for exmple if I am plotting x=y, after plotting I want to be able to clicking on graph and then I get dots on graph at (1,1) (2,2)(-1,-1) and .I am not sure if th
30、ere be anyawy to doing this?(I am really newcomber in Matlab GUI)I appreciate your help.Thank you,Arash18. on 23 Jul 2008 at 1:24 pm 18Quan QuachHi Arash,Use the data cursor icon to do this. See the last picture in this tutorial. The icon inside the right of the red box is the one you want to use. C
31、lick on this and then click on the curve.Quan19. on 24 Jul 2008 at 9:22 am 19ArashHi Quan,Thank you, I didnt get chance to try yet, I will let you know.appreciate your help.Thank you,Arash20. on 30 Jul 2008 at 7:55 am 20ArashHi,Thank you, it really helped, but is it any way to get coordination with
32、out using tool bar, I mean is it any way to write function to do that, having more control on tool bar, for instance if we have to plot, so we have control some how to check coordination of either one at a time and cursar be regular cursar for other one?Thank you,Arash21. on 30 Jul 2008 at 8:10 am 2
33、1ArashHi,to give more, I am really new in Matlab but when I was reading tutorial for Radio Buttons, Toggle Buttons, and Button Group Panel I thought may be I can withset(handles.fontSelect_buttongroup,SelectionChangeFcn,fontSelect_buttongroup_SelectionChangeFcn);Create a function for getting coordin
34、ation and then put push button for my plot so when I click that button or checkbox my cursar goes to getting coordination mode and get that instead of using toolbar.and I can put 2 different checkbox or button for 2 different plot in my program .Thank you,Arash22. on 30 Jul 2008 at 1:16 pm 22Paul DA
35、rash, a way to get the cursor position in your program without using the toolbar is as follows.l=datacursormode;lData=get(l,DataCursors);pos=get(lData,Position);you can also set the cursor position by doing the above in reverse with set commands. Test it out and youll see that it works pretty well.P
36、aul23. on 30 Jul 2008 at 1:20 pm 23Paul DOr even l=datacursormode;pos=get(l.DataCursors,Position);24. on 30 Jul 2008 at 3:56 pm 24ArashHi paul,I creat 2 button for my 2 figure and put the code you gave me at call_back event of those but I get error.basically my goal is to control cursor some how whe
37、n I click the button for that plot when I move my mouse on figure I get coordination of that point, just like a time I use toolbar.Arash25. on 31 Jul 2008 at 6:56 am 25Paul Dim not sure exactly at what you are trying to accomplish. Here is a little go through that you can just copy and paste into ma
38、tlabs command prompt that will show the capabilities that I am discussing.x=0:.1:10;y=x2+2*x;graph=plot(x,y);l=datacursormode(graph);NOW goto the figure that just popped up and put a cursor position on the graph.NOW go back to the command prompt and typepos=get(l.DataCursors,Position)you will see th
39、at the variable pos holds the coordinates of the cursor.I do believe that this is the capability that you requested. This should be enough information to help you do whatever you want to do w/ captureing the coordinates of a cursor.26. on 10 Aug 2008 at 3:50 pm 26Ashutosh BhardwajHelped me a LOT wit
40、h GUI programming. you guys should come out with a book !27. on 19 Aug 2008 at 1:57 am 27NrThank you very much for this tutorial!Matlabs help files are very confusing for me every time.These tutorials are very helpful.28. on 29 Aug 2008 at 3:03 pm 28RomanHi Quan Quach, do you have a tutorial about w
41、riting a GUI that allows signals, lets say, from a signal generator through serial port, to be displayed on the gui?Thanks29. on 29 Aug 2008 at 9:53 pm 29Quan QuachHi Roman,I havent dealt with data acquisition using MATLAB. So cannot help you there. Hopefully I will use it in the future and I can wr
42、ite a tutorial on it! Quan30. on 24 Sep 2008 at 5:19 am 30DEVANANDHello Quan,What a nice work this site is.I am an M-beginner.I have a doubt.I have a GUIDE application. Now I wanted to uncheck and check the checkboxes by clicking a pushbutton. How can it be done , plz help me.THANKS IN ADVANCE31. on
43、 26 Sep 2008 at 2:28 pm 31sathyaDevanand,In the pushbutton callback, useset(handles.checkbox,Value,1)to check the checkboxand set(handles.checkbox,Value,0)to uncheck it.32. on 26 Sep 2008 at 2:34 pm 32Quan QuachDevanand,Sathya seems to hae beaten me to the punch. Thanks Sathya!Quan33. on 26 Sep 2008
44、 at 2:37 pm 33sathyaThanks to you Quan.For the past six months I have been working on MATLAB and your tutorials have helped me a lot.34. on 26 Sep 2008 at 2:39 pm 34Quan QuachGlad you found our site helpful! Quan35. on 29 Sep 2008 at 6:29 pm 35LiamFantastic tutorial. I was amazed that I could get a
45、small project working right off the bat based on this. However, I have 2 questions:1. Suppose I have two sliders and want to plot a point on the axes based on the values of the sliders. Which callback function should do the plotting? Whats the best way to approach this?2. Is there any way to turn of
46、f the automatic resizing of the axes? I want the axes to stay fixed regardless of the data being plotted.Thanks very much!Liam36. on 29 Sep 2008 at 8:51 pm 36Quan QuachHi Liam,Glad you found these tutorials helpful.for question 2:If you know what your x and y limits will be, you can do the following
47、:axis(xmin xmax ymin ymax)For question 1:You would want to put your plotting code in the callback for the slider widget. Every time the slider value is changed, the callback is activated. Note that the slider callback is only activated each time you click the mouse. Good luck!37. on 29 Sep 2008 at 9:09 pm 37LiamThanks, Quan. Ideally, Id like the axes to update if any of the sliders are modified. Right now it only updates if the slider within which the plot code is written is clicked on. Is there some sort of refresh plot command?One more question, how do you retrieve the layout window of a