How to retrieve data from the textboxes of a GUI created with GUIDE.

9 views (last 30 days)
How to retrieve data from the textboxes of a GUI created with GUIDE.
I am having trouble understanding the process of taking entered values from textboxes so that the values may be used in a process, e.g. plotting.
I have created a simple GUI with three textboxes (Tags: edit1, edit2 and edit3)
I want to retrieve the values entered and plot a simple graph. I have read through the Matlab notes and help guides and think that I need to use guidata, handles and set, but I can’t seem to get my head around how these commands interact.
The code below is obviously repeated for each textbox:
function edit1_Callback(hObject, eventdata, handles)
%get value as a string from text box and convert to double
number1 = str2double(get(hObject,'string'));
%check that value is a number and display error if it is not
if isnan(number1) || ~isreal(number1)
errordlg('Try Again','Try again','modal')
return
end
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
I ultimately want to plot a simple graph using something like:
x = [0 1 2]
y = [number1 number2 number3]
plot(x,y)
I obviously have a fundamental lack of knowledge on this subject so I would be very grateful if someone could take the time to give me a layman’s explanation of how this works or maybe point me in the right direction for some reading material.
Cheers
Steve

Accepted Answer

Jan
Jan on 28 Oct 2013
You can access the content of a textbox in any function that has access to the handles object. For instance if you want to access the content of the textbox tagged edit1 from a function called my_fun it would look something like:
function my_fun( hObject, eventdata, handles )
content_str = get( handles.edit1, 'String' )
Use guidata if you want to change the content of a handle. E.g. if you have a handle variable called my_var and you want to assign it a new value you would do
function my_fun( hObject, eventdata, handles )
handles.my_var = 5;
guidata( hObject, handles ); % if you omit this line, the new value will not be set!

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!