help with gui share data

2 views (last 30 days)
Adrian Lee
Adrian Lee on 17 Nov 2020
Commented: Adrian Lee on 18 Nov 2020
i read the link to share data among callbacks, but i still couldnt understand and apply it to my code
in the code and gui, i want to have the drawrectangle function print the position output to 4 editable columns (because i want the user to be able to edit the position with the columns too)
but i can't make the code transferable to other callback functions
can anyone explain to me and also give an example fix to the code?

Accepted Answer

Rik
Rik on 17 Nov 2020
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
GUIDE uses guidata, which you can think of as a sort of mat file you store with your figure. You can only save 1 variable, so it makes sense to use a struct, as you can use fieldnames to have some more information in your variable (as opposed to a cell, where it would matter in what order the variables are stored). GUIDE automatically generates the callback functions in such a way that the third input will be the dynamically loaded struct. That means you only need to store back data at the end of your callback function. If you do that, that data will be available to the other callback functions.
  5 Comments
Rik
Rik on 18 Nov 2020
For some reason you insist on sticking with GUIDE. I don't understand why you would want to, but that is your choice.
As the automatically generated documentation of the OpeningFcn already states: you already have the struct with all required data: handles. So your edit was close, except for the fact that you are overwriting the handles struct when you store your variable called data to the figure with guidata.
function v1_gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
%either this or handles.data.a and handles.data.a2, pick what makes sense for you
handles.a = 1+1;
handles.a2 = 2+2;
% Update handles structure
guidata(hObject, handles);
You don't need to use data=guidata(hObject); in your callback function as I already explained: the callbacks that are generated by GUIDE already do that.
The main problem with GUIDE is very visible here: you didn't bother reading the comments, because there are too many of them, and most are too complex (or, in the case of eventdata, wrong). You didn't read what the different inputs to your function mean. I would really encourage you to step away from GUIDE. As far as I can tell it has remained mostly unchanged for the last 20 years.
Adrian Lee
Adrian Lee on 18 Nov 2020
ah, this explanation is clear to me now
it isnt that i insist on sticking to guide, i have already started looking into appdesigner
i do not have a good grasp of handles, the share callbacks link do not really give a good explanation as only one example and giving the pros n cons of each method

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!