Getappdata and Setappdata not working within callback
Show older comments
Hi all,
I am having a problem where getappdata (within a callback) is not retrieving all of the information stored with setappdata.
I store the data like this:
setappdata(gcf, 'Subject', Subject)
which reports this (the appropriate data):
name: 'Testeroo'
folder: 'C:\Users\jx2\Dropbox\Cirxb\xg\inducti...'
device: 2
mode: 1
Settings: [1x1 struct]
Audio: [1x1 struct]
but when I getappdata like this
Subject = getappdata(gcf, 'Subject')
within a callback it reports this:
name: 'Testeroo'
folder: 'C:\Users\jx2\Dropbox\xb\Dxg\inducti...' (edited by me)
device: 2
mode: 1
Settings: [1x1 struct]
which is missing the Audio field. Any ideas?
Thanks!
EDIT! Here is more of the code so you guys will have a better chance of understanding. Thanks for all the help so far.
This function is called prior to to pressing a button in a GUIDE GUI.
function initSoundTest(handles)
set(handles.slMinLevel,'Enable','On')
set(handles.editMinLevel,'Enable','On')
set(handles.tbMinTest,'Enable','On')
Subject = getappdata(gcf, 'Subject');
sound_type_default=1;
Subject.Audio.maxLevel =max(Subject.Settings.sound.max_K, Subject.Settings.sound.max_B);
Subject.Audio.minLevel =min(Subject.Settings.sound.min_K, Subject.Settings.sound.min_B);
Subject.Audio.sound_type=sound_type_default;
setappdata(gcf, 'Subject', Subject)
createSounds(handles);
Subject.Audio.audioTimer = timer('TimerFcn', {@PlayAudio,gcf}, 'Period', ...
1, 'ExecutionMode', 'fixedRate', 'BusyMode', 'queue');
set(handles.editMinLevel, 'String', num2str(min(Subject.Settings.sound.min_K, Subject.Settings.sound.min_B)))
set(handles.slMinLevel, 'Value', min(Subject.Settings.sound.min_K, Subject.Settings.sound.min_B))
setappdata(gcf, 'Subject', Subject)
disp(getappdata(gcf,'Subject'))
disp(gcf)
The result of those disps:
name: 'Testeroo'
folder: 'C:\Users\jaw772\Dropbox\Circadian Lab\Dixon winning\inducti...'
device: 0
mode: 1
Settings: [1x1 struct]
Audio: [1x1 struct]
Figure (figure1) with properties:
Number: []
Name: 'SetupWizard'
Color: [0.9400 0.9400 0.9400]
Position: [135.6000 45 74.8000 30]
Units: 'characters'
Use get to show all properties
The part of the code that is called when a button is pressed AFTER that function is called (usually immediately after):
function tbMinTest_Callback(hObject, eventdata, handles)
% hObject handle to tbMinTest (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
button_state = get(hObject,'Value');
Subject = getappdata(gcf, 'Subject');
disp(getappdata(gcf,'Subject'))
disp(gcf)
if button_state == get(hObject,'Max')
set(hObject,'String','Running')
Subject.Audio.currentLevels = 'Min'; %%max or min??
Subject.Audio
start(Subject.Audio.audioTimer)
elseif button_state == get(hObject,'Min')
stop(Subject.Audio.audioTimer)
set(hObject,'String','Test')
end
setappdata(gcf, 'Subject', Subject)
NOTE: This causes an error! "the timer doesn't exist" basically.
The result of the disps and the unsuppressed output:
name: 'Testeroo'
folder: 'C:\Users\jaw772\Dropbox\Circadian Lab\Dixon winning\inducti...'
device: 0
mode: 1
Settings: [1x1 struct]
Figure (figure1) with properties:
Number: []
Name: 'SetupWizard'
Color: [0.9400 0.9400 0.9400]
Position: [135.6000 45 74.8000 30]
Units: 'characters'
Use get to show all properties
ans =
currentLevels: 'Min'
4 Comments
Geoff Hayes
on 4 Feb 2015
Tredwise - how many figures do you have? Are you sure that there is just the one so that gcf always refers to the same one? It may be better to try using the figure handle instead so that you always set and get from the same figure. (If you have more than one figure, then gcf may be changing to whichever one has focus and so the get may not necessarily refer to that which was set.)
Image Analyst
on 4 Feb 2015
What did you do differently this time? Despite you saying that the audio field is there, now it is not. Did you just make a copy and paste error?
TREDWISE
on 4 Feb 2015
Accepted Answer
More Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!