关于Matlab中GUI的Slider.docx
- 文档编号:12899288
- 上传时间:2023-04-22
- 格式:DOCX
- 页数:40
- 大小:113.76KB
关于Matlab中GUI的Slider.docx
《关于Matlab中GUI的Slider.docx》由会员分享,可在线阅读,更多相关《关于Matlab中GUI的Slider.docx(40页珍藏版)》请在冰豆网上搜索。
关于Matlab中GUI的Slider
MATLABGUITutorial-Slider
28Oct2007QuanQuach118comments20,071views
Introduction
InthisMatlabGUItutorial,youwilllearnhowtocreateandusetheslidercomponent.Slidersareusefulcontrolsforchoosingavalueinarangeofvalues.Commonusesarevolumecontrols,seekersformovieandsoundfilesaswellascolorpickers.Anexampleofasliderisshownbelow.
ThistutorialiswrittenforthosewithlittleornoexperiencecreatingaMatlabGUI(GraphicalUserInterface).Ifyou’renewtocreatingGUIsinMatlab,youshouldvisitthistutorialfirst.BasicknowledgeofMatlabisnotrequired,butrecommended.Matlabversion2007aisusedinwritingthistutorial.Bothearlierversionsandnewversionsshouldbecompatibleaswell(aslongasitisn’ttoooutdated).Let’sgetstarted!
CreatetheVisualAspectoftheGUI
1.StartupMatlab,andtypeguideinthecommandline.
2.ChoosetocreateanewGUIusingthe“BlankGUI(Default)”option.
3.Clickon
andaddanEditTextcomponenttotheGUIfigure.Next,clickon
andaddaSlidercomponentontotheGUIfigure.
4.DoubleclicktheEditTextcomponenttobringupthePropertyInspector.ChangetheStringpropertyto0,andchangetheTagpropertytosliderValue_editTextasshowninthefigurebelow:
5.Next,let’smodifythepropertiesoftheSlidercomponent.FirstletssittheMinpropertyto0,andtheMaxpropertyto100.Next,changetheTagpropertytoslider1.
6.Here’swhatyourfigureshouldlooklikeafteryouaddthecomponentsandmodifythem.
7.Atthispoint,youalsomightwanttoaddsomeStaticTextcomponentstospecifytheminandmaxvaluesoftheslider.YoucanmodifytheirtextbydoubleclickingonthecomponentandchangingtheStringproperty.It’snotrequired,butIhighlyrecommendit.
8.SaveyourGUIwhereveryoupleasewithyourdesiredfilename.
WritingtheCodefortheGUI
Matlabautomaticallygeneratesan.mfiletogoalongwiththefigurethatyoujustputtogether.The.mfileiswhereweattachtheappropriatecodetothecallbackofeachcomponent.Forthepurposesofthistutorial,weareprimarilyconcernedonlywiththecallbackfunctions.Youdon’thavetoworryaboutanyoftheotherfunctiontypes.
1.Openupthe.mfilethatwasautomaticallygeneratedwhenyousavedyourGUI.IntheMatlabeditor,clickonthe
icon,whichwillbringupalistofthefunctionswithinthe.mfile.Selectslider1_Callback.
Addthefollowingcodetothefunction:
%obtainstheslidervaluefromtheslidercomponent
sliderValue=get(handles.slider1,'Value');
%putstheslidervalueintotheedittextcomponent
set(handles.slider_editText,'String',num2str(sliderValue));
%Updatehandlesstructure
guidata(hObject,handles);
2.Now,let’saddthefollowingcodetotheslider_editText_Callbackfunction:
3.%getthestringfortheeditTextcomponent
4.sliderValue=get(handles.slider_editText,'String');
5.
6.%convertfromstringtonumberifpossible,otherwisereturnsempty
7.sliderValue=str2num(sliderValue);
8.
9.%ifuserinputssomethingisnotanumber,oriftheinputislessthan0
10.%orgreaterthan100,thentheslidervaluedefaultsto0
11.if(isempty(sliderValue)||sliderValue<0||sliderValue>100)
12.set(handles.slider1,'Value',0);
13.set(handles.slider_editText,'String','0');
14.else
15.set(handles.slider1,'Value',sliderValue);
end
16.Saveyourm-file!
RunandTesttheGUI
Nowthatwe’vecompletedboththevisualandcodeaspectsoftheGUI,itstimetoruntheGUItomakesureitworks.
1.Fromthem-fileeditor,youcanclickonthe
icontosaveandruntheGUI.Alternatively,fromtheGUIDEeditor,youcanclickonthe
tolaunchtheGUI.ThefollowingGUIshouldappearonceyouclicktheicon:
2.Now,trytoputindifferenttypesofinputstotesttheGUI.Anyinputthatisnotanumber,lessthanzero,orgreaterthan100shoulddefaulttheslidertoavalueofzero.
3.Andthat’sit.ThosearethebasicsofusingaSlidercomponent.YoucanexploretheotheroptionsthatthesliderhastoofferthroughthePropertyInspector.Forinstance,youcanusetheSliderSteppropertytocustomizehowfaryouwanttheslidertomovewhenyoupresstheleftandrightarrow,orwhenyouclickonthescrollbar.
Thisistheendofthetutorial.
Sourcefilescanbedownloadedhere.
118Responsesto“MATLABGUITutorial-Slider”
1.on05Dec2007at1:
10pm1Waqas
Thiswasveryhelpfull,thnx4helpingmeout!
2.on20Jan2008at4:
28pm2Alex
Thanks!
!
!
!
Amdoingcoursework-veryhelpful…
3.on30Jan2008at2:
44pm3Graham
Verynicemate,clearlypresentedandeasytofollow,10/10
4.on18Mar2008at3:
17am4Eman
ThanksforthegreattutorialsIhavebeenlivingonyourtutorialsforthepastfewdaysandtheyarehelpingmedrivethrough…Greatworkandeffort….
Ihavesomeinquiriesabouttheslider,
Iwouldliketohaveaminvaluethatisnotzeroontheslidersuchthattheextremeleftisa1forexample.InadditionIwouldliketoknowhowIcouldsettheinitialvalue/Positionoftheslider.
Thanksforyourhelp
5.on18Mar2008at5:
48pm5DanielSutoyo
HeyEman,thanksforthesupport…herearetheanswerstoyourinquiries
1.YoucanchangeminvalueofthesliderbygoingtoGUIbuilderanddoubleclickontheslidertohavethepropertiesmenushowup.ScrolltoMin,andchangethevalueform0to1.
2.Tosetinitialvalue,hereisonewaytodoit.PastethefollowingcodeintheSlider_OpeningFcn.Itbasicallysetsyourslidervalueandthetextboxtoaninitializedvaluethatyoudetermine.
initval=10;
set(handles.slider1,’Value’,initval);
set(handles.slider_editText,’String’,num2str(initval));
6.on20Mar2008at7:
13am6Eman
ThanksalotforyourhelpItsworkingFinenow
7.on22Mar2008at12:
22pm7rajo
thanks!
thesetutorialsarehelpful
iwouldliketousethematlabGUIfordrawingagraphofECGsignal(only50among500elementsofthedatashouldbedisplayedfirst)andtouseaslidersothatwhenuserclickthesliderotherpartsofgraphshouldbedisplayedjustinthesamewayweusethenormalslider.Asuserpressuppartofthesliderapagemovesupandviseversa.ihopeyouwillhelp
8.on25Mar2008at10:
12pm8Bravo
HowmultilinestatictextcanscrollusingsliderinGUI.Actuallyihaveabigoutputtoshowonstatictext,notfessibletodisplayonassignedplaceofSatictext.Scrollingrequiredhorizotallyandvertically.IrequiredtutorialasiamnewinMatlab.Thanksinadvance.
9.on01Apr2008at7:
47am9Yiannis
Niceworkthere!
Isthereanywayofdisplayingthecurrentvalueoftheslider,onit,orasideofit?
10.on09Apr2008at7:
16am10beaf
Yiannis==>Youcanaddanother“edittext”withaverylittlefontsize.
Then,youputitjustunderyoursliderandyourepeatthetutorialstepsto“connect”thesliderandthenew“edittext”.
sorryformyenglish
11.on17Apr2008at6:
37am11Paresh
NiceArticle
12.on12May2008at1:
32pm12Dushyant
extremelygoodandsimpletounderstandtutorials..notjustthisone…allofthem…
thankyou!
!
13.on11Jun2008at3:
54pm13forrest
Haveyoufoundawaytomakethesliderupdateconstantly?
Irealizethiswouldcreateloadsofcallbacks,butIfinditnecessaryinsomeinstances.I’vefoundnodocumentationtosupportthisfeature.
14.on17Jun2008at6:
28am14Martin
Realygoodworkyoudonehere.Helpsalot.
I’vegotaproblemandIwonderifyougotanideahowtosolveit.
I’musingasliderthatshouldcovertheareafrom0-63andI’monlyinterestedinintegers.I’veconnectedittoaneditasyoudescribed.
I’vefoundoutthatIshoulduse1/64assliderstepbutthepropertyinspectorolytakesthedecimalsandthatsgiveme1/64~0.016wichisn’tgoodenough.
Doseanybodyknowhowtosolvethis?
Thanks…
15.on17Jun2008at6:
52am15Martin
Sorryaboutmyunreadablewriting.
I’vefoundoutthatIshoulduse1/64assliderstepbutthepropertyinspectoronlytakesthreedecimalsandthatgivesme1/64~0.016wichisn’tgoodenough.
16.on18Jun2008at2:
22am16Albert
Yes,youhavetousedecimalfrom0to1whichis‘1/(max-min)’.Whatyoucandoisusewincalctodeterminewhatthestepisandcopyandpastetheanswerintothepropertyeditorforthesliderxandyvalues(xisforthedirectionbuttonsofthesliderandyisforthetrough).Whenyouclickonit,itwillshowthefullvalue(0.015625)untilyouclickawaythenitwillprobableshowwhatyouareexperiencing.Thisisnotaproblem,itshouldstillworkfine.
Inthecallbackfortheslider,youmaywanttoensurethatyouaregettingintegersbyusingsomethinglikethefollowing:
IfyouhaveaneditboxcalledtextInputandaslidercalledtextInputSlider,
fortheslider
set(handles.textInputSlider,‘String’,floor(get(hObject,’Value’)));
17.on18Jun2008at8:
18am17Martin
Thanks
18.on29Jun2008at8:
06am18AbdullahiMohammed
Hello,
Yourwebsitehelpedmealot.Idonotknowhowtosaythankyou.IhavereadquitealotoftextbooksonGUI.ButthefinalimplimentationoftheGUIhasnotbeenproperlyexplainedexceptyou.Lookingforwardtoreadmoreeducativeitemsfromyourwebsite.
19.on10Jul2008at7:
15pm19kaspi
soideclaredaglobalvariablecalled“wavelength1″inbothedittextandslidercallbacks.theniset“wavelength1=sliderValue”sothatthetwoarelinked.myproblemisthatwhenigotoenteravalueintheedittextandthenhitenteritdoesacarriagereturnratherthanchangingthepositionoftheslider.ifoundthatifienteravalueintheedittextandthenclicksomewhereelse,thesliderchangesposition.buthowcan
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 关于 Matlab GUI Slider