我正在做一个做一些信号处理的gui。其中一个组件是“记录”按钮,它记录麦克风几秒钟的声音。一些滑块后来以不同的方式移动它,后来有一个播放转换声音的播放按钮。
我使用手柄获取有关从滑块中选择的频率的所有信息,它工作正常。但是由于录制的声音,我有点担心,因为我知道句柄结构一直被复制。几秒钟的声音可能不会太多,但我担心性能问题,因为gui运行了很长时间并且很多组件被点击了。我在一些Mathworks页面上读到guidata / handle不应该用于大型数据结构,因为它被复制了。我按照https://se.mathworks.com/help/matlab/ref/getappdata.html
的说明尝试使用setappdata和getappdata在这里 https://se.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html#bt9p4t0
虽然它没有用,但给了我一个
Attempt to reference field of non-structure array.
Error in gui>recordbutton_Callback (line 334)
setappdata(hObject.Parent, 'v', 'Record')
错误。
这是我尝试存储它的方式:
% --- 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)
我已经尝试将它存储在hObject.Parent和hObject本身中。两者都给出了相同的错误。我希望数据可以从所有其他组件的回调函数访问,但不能一直复制。 请注意,在这个例子中,我只是尝试将字符串'Record'存储在变量v而不是记录中以便于阅读。
编辑:我正在使用GUIDE。
答案 0 :(得分:2)
要在r2014a和r2016b中完成这项工作,您有两种选择:
让r2014a像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)
在两者中使用r2014a语法:
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)