How to use variables in between GUI function?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am very new to GUI and so far for my understanding is that you can only put a tag's name behind handles. to communicate between different functions. For example, one uses axes(handles.axes1) inside a push button function.
For my case, I have several functions inside a GUI and there is one variable that is produced by one function, which is not defined previously when I made the GUI layout by GUIDE, it is only produced when the code is run and I would like to use it later in a pushbutton function, in this case, what should I do to make this variable outside of its mother function in order to be used in my pushbutton function?
Sorry if I sound confusing.
Any discussion/help is welcomed! Thanks!
採用された回答
Azzi Abdelmalek
2016 年 7 月 25 日
your_variable=10
handles.your_variable=your_variable
guidata(hObject,handles)
To call the variable
your_variable=handles.your_variable
8 件のコメント
chlor thanks
2016 年 7 月 26 日
This is the code I tried
%something
set(handles.file,'String','1.xls');
guidata(hObject, handles);
function Pushbutton1_Callback(hObject, eventdata, handles)
filecall = handles.file
winopen(filecall)
But it returns me errors of such
Reference to non-existent field 'file'.
Error in exercise>exercise_OpeningFcn (line 92)
set(handles.file,'String','1.xls');
%change this line
Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [],
guidata(gui_hFigure), varargin{:});
Error in exercise(line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)exercise('Pushbutton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
chlor thanks
2016 年 7 月 26 日
Wait! So I tried
file='1.xls';
handles.filecall=file
guidata(hObject, handles);
instead of
set(handles.file,'String','1.xls');
guidata(hObject, handles);
And it worked fabulously! Thank you Azzi! By the way, do you mind explaining to me why this made such a difference?
Adam
2016 年 7 月 26 日
Presumably handles.file is not a GUI handle. You can't use the 'set' syntax to create a field on your struct. If you want handles.file to just be a variable then use
handles.file = '1.xls';
chlor thanks
2016 年 7 月 26 日
Thanks Adam! So by GUI handle you mean the tags that were created from the CreateFcn? And since this is does not belong to the GUI layout, we cannot set it but what about handles.filecall? Doesn't this presume that filecall is a handle?
handles is just a simple struct, it is nothing magic. The fields that get added to it during creation of the GUI are the GUI components, these are graphics objects, each of which support the set syntax (though now since R2014b they also support the dot based class syntax) and these are the things that will have callbacks attached and which you interact with in those callbacks using your tags.
However, because handles is just a struct, you can add any field you want to it just like any other struct. These fields can be whatever you want - new graphics objects that weren't defined in GUIDE, cell arrays, user classes, simple scalar doubles, etc.
The 'magic' comes from the guidata( hObject, handles ) line which saves this handles struct back into your GUI so that next time you trigger a callback this version of the handles struct is passed to it rather than the one that was present immediately after creation of the GUI. If you miss this line out then your changes were just made to a local copy of the handles structure which goes out of scope when the current function ends.
chlor thanks
2016 年 7 月 26 日
I think I understands what you mean better now, so handles is a struct that you can add any field by using handles.tag/variable_name=something. You can only use set for an existing handle. And its best to use guidata(hObject, handles) to end your OpeningFcn and all your CallbackFcn that have handles defined in. My other question is, if you create your own function within GUI that generated new handles, do you need to end this function with guidata(hObject, handles) as well?
Thanks Adam, I am a newbie with GUI and your comments explain a lot.
Adam
2016 年 7 月 27 日
Anything that changes non-graphics objects data on the handles struct will need a call to guidata after it in order to save it back into the main GUI.
If you have factored out a function from a callback or are using listeners you may not have 'hObject' though. In this case you can use gcbo instead or pass the handle of the main GUI down to your function and use
handles = guidata( hGUI );
to extract the handles struct, hGUI being the handle to your main GUI.
chlor thanks
2016 年 7 月 27 日
This makes sense, thanks for the tips! I will keep them in mind.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
