Greetings.
I am trying to add a user defined variable to the "hObject" Handles via two radio buttons.
The radio button calling is here:
set(handles.boardsize,'SelectionChangeFcn',@boardsize_SelectionChangeFcn);
The actual code that uses "setappdata is here:
function boardsize_SelectionChangeFcn(hObject, eventdata)
%retrieve GUI data, i.e. the handles structure
handles = guidata(hObject);
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object
case 'button5800'
%execute this code when 5800 is selected
setappdata(hObject,'boardtype',0)
disp('this is working')
case 'button9800'
%execute this code when 9800 is selected
setappdata(hObject,'boardtype',1)
disp('this is working')
otherwise
% Code for when there is no match.
end
%updates the handles structure
guidata(hObject, handles);
Then later on I try and get the add data within the main function
boardsize = getappdata(hObject,'boardtype')
But the board type variable is always empty "[]" Am I updating the wrong handles? This one has had me for a while. Thanks to all who look at it.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Okt. 2011

1 Stimme

setappdata() and getappdata() do not take the handles structure as their first argument. They take the handle of a single graphics object, and they act with respect to that exact graphics object.
This differs from guidata() in that guidata climbs the hierarchy of the object given to it in order to find the ancestor figure and attaches the app data to that figure.
If you want to attach app data to your figure, find the figure first. For example,
setappdata(ancestor(hObject,'figure'),'boardtype',1);

4 Kommentare

Fangjun Jiang
Fangjun Jiang am 12 Okt. 2011
Thanks Walter for pointing out my mistake. I'll correct it in my answer.
William
William am 12 Okt. 2011
Thank you for the answer. What does "ancestor" mean?
Walter Roberson
Walter Roberson am 12 Okt. 2011
http://www.mathworks.com/help/techdoc/ref/ancestor.html
William
William am 12 Okt. 2011
I cannot figure this out. Is there an easier way to pass variaables from one function to another? I tried using global but I cannot get the variable itself to become actually "global"
Thanks again

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Fangjun Jiang
Fangjun Jiang am 12 Okt. 2011

3 Stimmen

I believe it is setappdata(handles,'boardtype',0), not setappdata(hObject,...), same for getappdata().
EDIT: My above answer is incorrect. See Walter's answer.
The correct approach should be:
  • Use setappdata(hObject,'boardtype',0) as you did inside function boardsize_SelectionChangeFcn(hObject, eventdata)
  • Within the main function, use boardsize = getappdata(handles.boardsize,'boardtype')
Another way to store the data for the object is to use the 'UserData' property of the object.

5 Kommentare

Iman Alsharkawi
Iman Alsharkawi am 12 Okt. 2011
Fangjun is right. The first argument is the handle of the object, not the object itself.
Fangjun Jiang
Fangjun Jiang am 12 Okt. 2011
@Iman, I don't understand your comment.
I was trying to point to William that app data is usually attached to handles so it's easy to pass among different GUI objects. The app data could be attached to hObject but it will be harder to pass the app data to other GUI object.
William
William am 12 Okt. 2011
Thank you gentleman I commented on that but it states
??? Error using ==> getappdata
Conversion to double from struct is not possible.
Error in ==> DataVisualizer>update_Callback at 171
boardsize = getappdata(handles,'boardtype')
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> DataVisualizer at 47
gui_mainfcn(gui_State, varargin{:});
??? Error while evaluating uicontrol Callback
Fangjun Jiang
Fangjun Jiang am 12 Okt. 2011
The error message indicates that you have a data type conversion problem. Put a break point at line "boardsize = getappdata(handles,'boardtype')", when it stops at that line, just run class(getappdata(handles,'boardtype')) in Command Window to see what is the data type.
Fangjun Jiang
Fangjun Jiang am 12 Okt. 2011
Sorry for giving you bad advice. Now Walter has pointed out the problem. The error message makes sense. "handles" is a structure containing handles of different GUI objects. Running getappdata(handles,'boardtype') will make it try to convert "handles" (which is a structure) into an object handle (which is a double).

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by