user1661303
user1661303

Reputation: 539

Storing/passing semi-large data in GUIDE, Matlab

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.

Upvotes: 1

Views: 204

Answers (1)

matlabgui
matlabgui

Reputation: 5672

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)

Upvotes: 2

Related Questions