关于Matlab中GUI的Sharing Data among Callbacks and Sub Functions.docx
- 文档编号:4374160
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:28
- 大小:79.47KB
关于Matlab中GUI的Sharing Data among Callbacks and Sub Functions.docx
《关于Matlab中GUI的Sharing Data among Callbacks and Sub Functions.docx》由会员分享,可在线阅读,更多相关《关于Matlab中GUI的Sharing Data among Callbacks and Sub Functions.docx(28页珍藏版)》请在冰豆网上搜索。
关于Matlab中GUI的SharingDataamongCallbacksandSubFunctions
MATLABGUITutorial-SharingDataamongCallbacksandSubFunctions
14Nov2007QuanQuach69comments14,141views
Introduction
ForthemajorityofGUIsthatyoucreateinMatlab,youwillhavetobeabletosharedataamongthecomponentcallbacks.Onewayofaccomplishingthisistousethehandlesstructuretostorethatdata.Inthistutorial,youwilllearnhowtodoso.
ThistutorialiswrittenforthosewithlittleexperiencecreatingaMatlabGUI(GraphicalUserInterface).Ifyou’renewtocreatingGUIsinMatlab,youshouldvisitthistutorialfirst.BasicknowledgeofMatlabisrecommended.Matlabversion2007aisusedinwritingthistutorial.Bothearlierversionsandnewversionsshouldbecompatibleaswell(aslongasitisn’ttoooutdated).Let’sgetstarted!
SharingDataBetweenCallbacks
AsimpleexampleofsharingdataamongcallbacksistoincrementanumericalcounterontheGUIeachtimeANYbuttonontheGUIispressed.Howcanthisbedone?
Thistutorialwillexplainhow.
1.First,downloadthesampleGUIhere.Unzipthefilesandplacethemwhereveryouplease.
2.Now,typeguideatthecommandprompt.
3.ChoosetoopenthesampleGUIbyclickingon“OpenExistingGUI”.Clickon“Browse”tolocatewhereyousavedtheGUIfiles.
4.HereiswhattheGUIshouldlooklikewhenyouopenit:
5.Clickonthe
iconontheGUIfiguretobringuptheaccompanying.mfile.
6.Thefirstthingweneedtodoistodefineavariablethatwillholdthevalueforournumericalcounter.Placethefollowingcodeinhandles_tutorial_OpeningFcn.
7.handles.buttonCounter=0;
set(handles.text1,'String','0');
Makesureyouplaceitrightbeforethislineofcode:
guidata(hObject,handles);
8.Noticethatwedefinedthevariablenameashandles.buttonCounter,ratherthanbuttonCounter.Ifwedidnotdefinethevariablewithinthehandlesstructure,thenitwouldbeconsideredalocalvariable.Beingalocalvariablemeansthatthevariabledoesnotexistoutsideofthefunctionwhereitwasdefined.Thusweneedtostoreitintothehandlesstructure,sothatitcanbeaccessedandmodifiedlaterintheothercallbacks.So,ifyouwantdatatobeavailabletoALLcallbacks,youmuststoreitinthehandlesstructures.
9.Addthiscodetobothpushbutton1_Callbackandradiobutton1_Callback
10.handles.buttonCounter=handles.buttonCounter+1;
11.set(handles.text1,'String',num2str(handles.buttonCounter));
12.guidata(hObject,handles);%withoutthisline,thehandlesstructurewouldnotupdate,
%andthecounterwouldnotincrement.
13.Now,savethe.mfileandruntheGUI.Tryclickingonthebuttonstoincrementthecounter.NoticethatthecounterincrementswhenyouactivateANDdeactivatetheradiobutton.
SharingDataBetweenCallbacksandSubFunctions
Itisgoodpracticetomakeyourfunctionsmodularsothatyourcodeiseasilyadaptable,robust,andflexible.Havingsaidthat,passingtheentirehandlesstructurestosubfunctionsissomethingthatIdon’trecommend.Buttheremightbeatimewhenyouneedtoaccesstheentirehandleswithinasubfunction.Soheregoes.
function[handles]=mySubFunction(handles)
%insertcodehere
%
%
Thisfunctionwillallowyoutousethehandlesdatawithinthesubfunction,andwillalsoupdateanychangesmadewithinthesubfunction.Youcanaddasmanyinputsandoutputsasdesired.
Whenyoucallthisfunction,makesuretocallitinthefollowingmanner:
[handles]=mySubFunction(handles);
Thisistheendofthetutorial.
69Responsesto“MATLABGUITutorial-SharingDataamongCallbacksandSubFunctions”
1.on20Nov2007at10:
02am1manisree
greattutorials…indeedveryhelpful.thanksalot
2.on18Feb2008at11:
36am2Vishal
Ivejustspentthelast2hourspullingmyhairout,asimoneofmyfunctionswasreadinginvaluesfor2variables,andithenlatercallafunction(ifdesired)toplotaPZmap,anditmatlabrefusedtounderstandthatthevariableswherethere,untilirealisedthattheywherejuslocalvariables!
Itriedallsortsofhandlesstuff,untilifoundthis,manyouguysarethereasonimightgetagoodgradeformyfinalyearproject!
!
!
3.on15Mar2008at10:
00am3Brinkis
HalloQuanQuach,
I’vepassedthoughallofyourtutorials.They’rereallygreatandhelpfull.AtfirstIwouldliketogiveabigthanksforthemandaroundofcyber-beer
Theideaofusingglobalvariableshelpedmealot.
AndsecondIwouldliketoaskforinformationhowtosolvedifferentialequationscorrectly.Ifoundeditdifficult,pleasehelp
IhavetomakeaGUIthatsolvesmaxwell’sequationsindifferentialformfor~1kkpointsandplotsresultsin3Dgraphic.It’sEMwavecomputingusingFDTDmethod.Anyhelpwillbeappreciated.Thanksinadvance
4.on15Mar2008at1:
54pm4QuanQuach
HiBrinkis,
IwouldprobablyuseSimulinktomodelthedifferentialequationandthenhavetheGUIincorporatetheSimulinkmodel.Unfortunately,I’mnotverygoodwithsimulink.
Here’sagreattutorialonusingsimulink:
Visitthistutorialonceyoucompleteyourmodel!
5.on19Mar2008at10:
37am5Brinkis
HalloQuanQuach,
I’vefailedtofoundsimulinkfordownload
andmymatlab2007don’tcontainit
AfterI’veredthesetutorialsaboutsimulinkI’vefiguredoutthatsimulinkissomethinglikeatoolformaking“logicalschema”.
AllIneedistowriteacodeformatlabtoperformacycle.Ineedmatlabtodescribematerialofeachpoint,thensolveequationtofoundanangleforradiationpatternrefersto(thedirectional(angular)dependenceofradiationfromtheantenna)andthensolve6differentialMaxwell’sequationstofindthevalueofelectromagneticalfieldinthatpoint.Andwheneverythingisdone-plotresultsin3Ddiagram(pointsshouldbe2Dandaddingvalueofeverypointweshouldget3Dview).
ThepointI’mleadingtoisthatIalreadyknowhowthisshouldlooklikeinmatlab,butcan’timaginehowtoperformitwithsimulink.QuanQuach,ifyouhaveanyexperienceinwritingcodewithdifferentialequations,pleasehelp
anyhintwillbeappreciated
cheers,
6.on19Mar2008at10:
39am6QuanQuach
HelloBrinkis,
I’mveryrustywithdifferentialequationswithinmatlab,soIwon’tbeofmuchhelpthere.OneofthesedaysIwilluncoverthatrustandwriteatutorialondifferntialequationsandmatlab.
Quan
7.on19Mar2008at11:
00am7Brinkis
Thanksforsuchaquickreply
Andthanksforyourtutorialaboutdifferentialequationsinadvance
I’llworkonsolvingPartialdifferentialequationeithertohavemoreexperiance
8.on21Mar2008at11:
27am8Belo
helloQuanQuanch,
ifollowedyourtutorial(verygood…)forcreatingglobalvariables.
ifiwanttodeleteoneofthis,whatshouldido?
?
?
?
thanksalot……
9.on21Mar2008at1:
08pm9DanielSutoyo
HeyBelo
Youcantypecommand“whos”toseethevariablesyouhaveinworkspacethenuse“clearvariablename”toeliminatethespecificvariable
10.on21Mar2008at2:
33pm10Belo
thanksDaniel,
itriedit,butitdoesn’twork..
ithinktheproblemisthatthevariableisn’ta‘true’variablebutisapartofthehandlesstructure(orsoiunderstand….).
11.on21Mar2008at3:
48pm11DanielSutoyo
ahifyouaretalkingabouthandlesstructureyesitwouldbedifferent
usethecommand
rmfield(handle,‘name’)
toremove
handle.name
12.on23Mar2008at12:
57pm12Belo
thanksalot….
that’swhatineed
13.on02Apr2008at4:
44am13AdamBright
Hithere,
ihavecreatedtwolistboxeswhichwillbethestarttimeandendtimewhichiwanttoappearasthexaxisonmygraph.Atthemomentmyscriptreferstoanarraycalled‘time’asthex-axis.
Iwouldliketobeabletoselectthestartandendtimefromthesetwolistboxesandthenpressthegraphbuttontoproduceagraphforthatrange.
Ihavenoideahowtolinkthelistboxtothearray‘time’orhowtomakethescriptihavemaderefertothesedateswhichhavebeenchosen.
I’mreallystuckandwouldbegratefulforanyhelp.
manythanks
%AssingingVariables
[time,number,pressure,raininput]=textread(’staticvirginia.txt’,'%s%d%f%f’,'delimiter’,',’,'headerlines’,4);
%Ifstatementtoconvertpressureintovolume
case1=(pressure=995.7&pressure=1149.2);
volume=zeros(size(pressure));
volume(case1)=((pressure(case1)-442.95)*5.365);
volume(case2)=((pressure(case2)-749.29)*12.034);
volume(case3)=((pressure(case3)-934.08)*22.371);
cumvol=volume;
fori=2:
length(cumvol),ifcumvol(i) zerovol=(cumvol-(cumvol(1,1))); %values=3000*(ones(size(zerovol))); runoff=zerovol/3000; cumrain=cumsum(raininput); %5minuterainvalues cumrain5=cumrain(1: 5: end); runoff5=runoff(1: 5: end); %Discretisedvaluesforcumrainandrunoff d=2: length(cumrain5); disccumrain=(cumrain5(d))-(cumrain5(d-1)); e=2: length(runoff5); discrunoff=(runoff5(e))-(runoff5(e-1)); %GraphPlottingInformation timeplot=datenum(time,‘”yyyy-mm-ddHH: MM: SS”‘); timeplot5=timeplot(3: 5: end); [AX,H1,H2]=plotyy(timeplot5,disccumrain,timeplot5,discrunoff,’bar’); set(gcf,‘color’,‘white’); set(H1,’BarWidth’,0.4,… ‘EdgeColor’,[00.7490.749],… ‘FaceColor’,[00.7490.749],… ‘LineStyle’,'–’) set(H2,’BarWidth’,0.4,… ‘EdgeColor’,[0.68240.46670],… ‘FaceColor’,[0.68240.46670],… ‘LineStyle’,': ’) set(AX (2),’YLim’,[01]) set(AX (1),’YLim’,[01]) set(AX (1),’YTick’,[00.10.20.30.40.50.60.70.80.91]) set(AX (2),’YTick’,[00.10.20.30.40.50.60.70.8
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 关于Matlab中GUI的Sharing Data among Callbacks and Sub Functions 关于 Matlab GUI Sharing
链接地址:https://www.bdocx.com/doc/4374160.html