MATLAB Answers

Using Push Button to Export a String to an Edit Textbox (GUI)

8 views (last 30 days)
Thomas Côté
Thomas Côté on 12 Jun 2019
Answered: G A on 12 Jun 2019
Hello all, as simply as possible, how would you export a string to an Edit Textbox so it can be displayed when the GUI is run?
Would you use a combination of setappdata, getappdata, sprintf()? I'm new to GUI and would really like a good universal "recipe" or method that can be easily adapted for other similar purposes (say, export cell arrays from a listbox to an axes1 graph, etc.).
For example, my string is "Name" here below in my Push Button Callback function :
function pushbutton4_Callback(hObject, eventdata, handles)
Name = 'Thomas';
setappdata(handles.figure1, 'Name', Name); %Storing my string "Name" in my UI figure1.
And I would like to export this name and display it into my Edit Textbox (Callback function here) :
function edit1_Callback(hObject, eventdata, handles)
Name = getappdata(handles.figure1 , 'Name'); %Retrieving my string "Name"
set(handles.edit1,'String', Name); % Displaying my string "Name" into my Edit Textbox.
I'm probably getting this all wrong. I'm not sure what to use to display my string "Name". It should look like this button on the right and the Edit textbox on the left :
PLPL.png
Thanks for the support!

  3 Comments

G A
G A on 12 Jun 2019
set(handles.edit1,'String', Name); % Displaying my string "Name" into my Edit Textbox.
Put this line in your pushbutton4_Callback fuction
Thomas Côté
Thomas Côté on 12 Jun 2019
It worked! Thanks a lot.
If you put your response in the answer section, I'll accept your answer :)
Rik
Rik on 12 Jun 2019
You mention in one of your code comment that you have a UI figure. That suggests you're using AppDesigner (which uses uifigure), while the rest of your code implies you're using GUIDE. If you are using the latter I have some advice on how to stop using it. It has an outdated structure with a lot of bloat that is likely reducing your efficiency (e.g. it still warns that eventdata is reserved for a future release, despite it being in use for years).
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
I will leave the answer section for G A.

Sign in to comment.

Accepted Answer

G A
G A on 12 Jun 2019
set(handles.edit1,'String', Name); % Displaying my string "Name" into my Edit Textbox.
Put this line in your pushbutton4_Callback fuction

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by