_Warning: Error occurred while executing callback: [...] Reference to non-existent field 'wpt'. Why do i get this warning in my script?

1 view (last 30 days)
Hello, I made a GUI for recording audiosignals with my laptop-microphone and used two buttons (start and stop) and two axes (one for the raw sample and one for the waveflet packet transform). Now there is a Warning when I klick on the start button:
_Warning: Error occurred while executing callback:
Error using Audioaufnahme_GUI_20150811>audioTimer (line 131)
Reference to non-existent field 'wpt'.
> In IntervalTimer>IntervalTimer.onCustomEvent at 146
In IntervalTimer>@(source,data)obj.onCustomEvent(data.Type,data.Data) at 107
In Channel>Channel.onCustomEvent at 377
In Channel>@(source,data)obj.onCustomEvent(data.Type,data.Data) at 303_
Can someone give me an explanation why this warning comes up? And how can I fix it? Below is my code.
Greetings
% --- Executes just before Audioaufnahme_GUI_20150811 is made visible.
function Audioaufnahme_GUI_20150811_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Audioaufnahme_GUI_20150811 (see VARARGIN)
% Choose default command line output for Audioaufnahme_GUI_20150811
handles.output = hObject;
% -- Variablen
%define samplingrate (Hz) and resolution (Bit)
handles.Fs = 1000;
handles.Bita = 8;
%define windowsize (s)
handles.Fenster = 1;
%Level of WPT
handles.level = 2;
%get a recorder
handles.recorder = audiorecorder(handles.Fs,handles.Bita, 1);
%create a timefunction to get every f.e. 200ms data from the recorder
set(handles.recorder, 'TimerPeriod', handles.Fenster, 'TimerFcn', {@audioTimer, hObject});
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Audioaufnahme_GUI_20150811 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Executes on button press in StartButton.
function StartButton_Callback(hObject, eventdata, handles)
% hObject handle to StartButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
record(handles.recorder);
% --- the set timefunction of the recorder
function audioTimer(hObject, varargin)
hSamples = varargin{2};
handles = guidata(hSamples);
% -- to get the samples out of the recorder
samples = getaudiodata(hObject, 'double');
% -- gives a maximum lenght (handles.Fs*handles.Fenster) of the samples array.
% older elements will be deleted
SampleSize = size(samples,1);
% the reason for deleting old elements
if SampleSize > handles.Fs
samples(1:SampleSize -(handles.Fs*handles.Fenster)) = [];
end
handles.samples = samples;
% -- Plotting the data
plot(handles.Signal, handles.samples);
plot(handles.WPTSignal, WaveletPacketTransform(handles.wpt));
guidata(hSamples, handles);
% --- the function for the wavelet packet transform
function WaveletPacketTransform(varargin)
hWPT = varargin{3};
handles = guidata(hWPT);
wpt = wpdec(handles.samples,handles.level,'sym2');
handles.wpt = wpt;
guidata(hWPT, handles);

Answers (2)

Walter Roberson
Walter Roberson on 11 Aug 2015
Your function audiotimer calls
plot(handles.WPTSignal, WaveletPacketTransform(handles.wpt));
which tries to call WaveletPacketTransform passing in handles.wpt. In order for that to work, handles.wpt has to have been created already. But you create handles.wpt only inside WaveletPacketTransform(). If you had managed to run WaveletPacketTransform once before the audiotimer function was called, then handles.wpt would exist to be used to pass a value in to WaveletPacketTransform to created handles.wpt ...
In short, you need to initialize handles.wpt before the first time you invoke audiotimer.
Second problem: your code for WaveletPacketTransform assumes that it is being passed in 3 parameters. But it isn't: it is being passed in only handles.wpt . When you make a direct call to a routine such as in the syntax WaveletPacketTransform(handles.wpt) then the parameters you pass there are the only parameters that the routine receives. Two parameters are added automatically only to routines that are directly called as callbacks, such as your audiotimer routine.
Note too that your WaveletPacketTransform tries to guidata(hWPT) where hWPT has been assigned the third passed parameter, the one that you are expecting to receive handles.wpt apparently. But handles.wpt is not a figure or other graphics object, it is a wpdec result, so you cannot guidata() it.
Normally when you are directly calling a routine from an interrupt routine and the second routine needs to know the figure it is being invoked from, you would have the interrupt routine pass its own hObject and event to the second routine, so the second routine could figure out where it was being called from. But that doesn't work for a timer routine because the hObject for a timer machine is the timer object rather than any graphics object. You will need to have the audiotimer pass down hSamples as you have constructed that to be the graphics object that will be needed to pull out the handles.
Next problem:
plot(handles.WPTSignal, WaveletPacketTransform(handles.wpt));
assumes that WaveletPacketTransform returns a value. But you do not return a value from it. If you want to construct it as an graphics interrupt routine (as you have sort of styled it now) then it cannot return a value, but if you want to construct it as a utility routine that will never be set as a Callback, then go ahead and have it return a value.

Tobias Krail
Tobias Krail on 12 Aug 2015
Thank you for the detailed post. :)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!