How to use 'Edit Text' efficiently in a GUI?

4 ビュー (過去 30 日間)
Lisa Wu
Lisa Wu 2012 年 11 月 23 日
コメント済み: siddharth 2014 年 12 月 9 日
I notices that there are two ways to ues it;
The tag of edit1 is edit1.
Method 1:
function edit1_callback()
num1 = get(hObject,'string');
handles.num1 = num1;
guidatda(gcbo,handles); % why need this command?
function pshbtn1_callback()
y = handles.num1;
guidata(gcbo,handles); % why need it too?
Method 2:
function pshbtn_callback()
y = str2num(get(hObject,handles));
As shown above, what's the difference? And the effect of "guidata(gcbo,handles)" ? Thank you very much!
  2 件のコメント
Lisa Wu
Lisa Wu 2012 年 11 月 23 日
The Method 2 should be:
y = str2num(get(handles.edit1,'string'));
siddharth
siddharth 2014 年 12 月 9 日
the second method works just fine. Thanks

サインインしてコメントする。

採用された回答

Arthur
Arthur 2012 年 11 月 23 日
If you use guidata(gcbo,handles), you save the string of your textbox in the 'handles' structure; in your pshbtn_callback you subsequently read this value from handles again. I personally prefer method 2, where you read out the string of the textbox in the button callback. This has a few advantages:
  • you only use the handles structure for what it's supppost to (storing handles)
  • you retrieve the string at the very moment you need it (ensures that you have the string that is actually shown in the textbox)
Method 1 is not wrong though, it will work.
  1 件のコメント
Lisa Wu
Lisa Wu 2012 年 11 月 26 日
Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by