GUI. Handle variable.

3 次查看(过去 30 天)
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]);

采纳的回答

Stephen23
Stephen23 2018-6-8
编辑:Stephen23 2018-6-8
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]);
  4 个评论
Arooba Khalid
Arooba Khalid 2018-6-8
@Stephen Cobeldick hi, I’m collaborating on this with her and We have assigned all the 4 textboxes to their callback, but we want to call these variables in our push buttons.For example, after i assigned these variables in their callback when i go to a pushbutton i need to use the get function again, its not automatically using the variables
Stephen23
Stephen23 2018-6-8
编辑:Stephen23 2018-6-8
@Arooba Khalid: Using a callback with a uicontrol edit box (I doubt that you are using a text box) is possible, but probably not worth the bother (unless you specifically want to trigger an action that is caused by someone entering some text). If there is a button that starts a calculation then personally I would just get all the required String properties when that button is pressed (rather than using callbacks for each edit box), as this would be simpler to implement, easier to understand, and easier to debug. All of the edit box handles are available in handles, so it is quite straightforward to get the String property for each edit box.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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!

Translated by