1
votes

I'm making a gui that does some signal processing. One of the components is a "record" button, that records a few seconds of sound from the microphone. A few sliders later pitch shifts it in different ways and there is later a Play button that plays the transformed sound.

I use handles for all the information about frequencies selected from the sliders and it works fine, However with the recorded sound, I get a bit worried since I know the handles structure is copied all the time. A few seconds of sound might not be too much, but I'm worried about performance issues as the gui is run a long time and a lot of components get clicked. I read on some Mathworks page that guidata/handles shouldn't be used for large data structures because it gets copied. I tried using setappdata and getappdata following the instruction here https://se.mathworks.com/help/matlab/ref/getappdata.html

and here https://se.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html#bt9p4t0

It doesn't work though, and gives me an

Attempt to reference field of non-structure array.

Error in gui>recordbutton_Callback (line 334)
setappdata(hObject.Parent, 'v', 'Record')

error.

This is how I try to store it:

% --- Executes just before gui is made visible.
function gui_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 gui (see VARARGIN)

setappdata(hObject, 'v', '');

%% --- Other app-specific inits


% --- Executes on button press in recordbutton.
function recordbutton_Callback(hObject, eventdata, handles)
% hObject    handle to recordbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
setappdata(hObject.Parent, 'v', 'Record')
getappdata(hObject.Parent)

I've tried storing it both in hObject.Parent and hObject itself. Both gives the same error. I want the data accessible from all other compnents' callback functions, but not copied all the time. Note that in this example, I just tried storing the string 'Record' in the variable v instead of recording for ease of reading.

EDIT: I'm using GUIDE.

1
Is this MATLAB <2014b? The error is because hObject.Parent, hObject is not an structure - Ander Biguri
Yes, > version gives 8.3.0.532 (R2014a) so I would guess so. I saw the hObject.Parent thing here on Stackoverflow so I just copied it. Any workaround? Ulitmately, the code is supposed to be run at both 2014a and 2016 but it's not a strict requirement. - user1661303
Copy pasting code without knowing what it does! What a terrible terrible think to do. Mainly because you have no idea what happens if it fails and not all code in SO is right. - Ander Biguri
I mostly agree to what you're saying. I however just assumed hObject.Parent would be a field of hObject that would access the underlying figure which would store the data and be reachable from all components. Looking at examples at Internet and learning how it works is how programmers learn. In this case it didn't act as I suspected, which I also aknowledged, but I don't see anything wrong. And I didn't copy paste. I wrote a word. Copy pasting long sections of code you don't understand, sure. But trying out new keywords that you read out in a Stackoverflow answer is not that wrong IMO. - user1661303
Thanks. Wasn't sure. You never know on the Internet. - user1661303

1 Answers

2
votes

to make this work in both r2014a and r2016b you have two options:

Make r2014a work like r2016b:

function recordbutton_Callback(hObject, eventdata, handles)
% hObject    handle to recordbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hObject = handle(hObject);
setappdata(hObject.Parent, 'v', 'Record')
getappdata(hObject.Parent)

Use r2014a syntax in both:

function recordbutton_Callback(hObject, eventdata, handles)
% hObject    handle to recordbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Parent = get ( hObject, 'Parent' );
setappdata(Parent, 'v', 'Record')
getappdata(Parent)