MATLAB: GUI. Handle variable.

guihandles

Hi. Is this the right way to retrieve a variable from handle? I read the documentation and examples but still didn't manage to make it work. Pls, give me a hint whats wrong. Learning to use guide I'm trying to draw a line between 2 points inputted in the textbox.
Thanks.
function x1_Callback(hObject, eventdata, handles)
handles.X1=str2double(get(handles.x1,'string'));
guidata(hObject,handles);
function y1_Callback(hObject, eventdata, handles)
handles.Y1=str2double(get(handles.y1,'string'));
guidata(hObject,handles);
function x2_Callback(hObject, eventdata, handles)
handles.X2=str2double(get(handles.x2,'string'));
guidata(hObject,handles);
function y2_Callback(hObject, eventdata, handles)
handles.Y2=str2double(get(handles.y2,'string'));
guidata(hObject,handles);
function startDisplay_Callback(hObject, eventdata, handles)
handles.X1=xOne;
handles.Y1=yOne;
handles.X2=xTwo;
handles.Y2=yTwo;
plot([xOne, xTwo],[yOne,yTwo]);

Best Answer

Your use of guidata is perfect: calling it at the end of a callback function lets you save the handles structure, together with any new data that you have added to handles. Then this data is available to all of the other callback functions.
I have no idea what actions will trigger these callbacks, so you will need to check that the callback functions are actually being run. Note that using variable/fieldnames that only differ by case is not recommended: instead of x1 and X1 you should use more descriptive names, e.g. x1Obj and x1Num.
You have mixed up the allocation within startDisplay_Callback, you will actually need to allocate the values in handles to the variables that you want to plot, like this:
xOne = handles.x1Num;
yOne = handles.y1Num;
...
plot([xOne, xTwo],[yOne,yTwo]);