我想保存矩阵,这是通过图形用户界面按钮输入到一个3D矩阵。例如:我单击按钮1,将2-2矩阵放在3D矩阵的第一个切片中。我比点击按钮3和一个不同的2-2矩阵是放在第二片.就这样等等。我希望这是足够清楚的。我的问题是我不是一个有经验的程序员。我目前初始化了一个零矩阵,如下所示:
storageMatrix = ones(2,2,100);
setappdata(0, 'storageMatrix', storageMatrix);我已经在主脚本中添加了一个函数updateStorageMatrix,请注意,这还没有完成。
function updateStorageMatrix
storageMatrix = getappdata(0, 'storageMatrix');例如,当我查看按钮1的代码时,它看起来是这样的:
% --- Executes on button press in Add_Straightduct.
function Add_Straightduct_Callback(hObject, eventdata, handles)
%prompt command for k number and length
prompt = {'k0:','Length:'};
dlg_title = 'Straight Duct specs';
num_lines = 1;
SDelements = {'0','0'};
Straightduct = inputdlg(prompt,dlg_title,num_lines,SDelements)
%insert values in function
sizeStorageMatrix = size(getappdata(0,'storageMatrix')); %get size of the storage matrix
dimT = sizeStorageMatrix(1,3); %take the number of matrices
if dimT==1
straightDuct = straight_duct(str2num(SDelements{1}), str2num(SDelements{2}), Mach_Frange(1,1))
setappdata(handles.updateStorageMatrix,'storageMatrix', storageMatrix(1:2, 1:2, 1))=straight_duct(str2num(SDelements{1}), str2num(answer{2}), Mach_Frange(1,1))
dimT+1
else
setappdata(0,'storageMatrix', storageMatrix(1:2, 1:2, dimT+1))=straight_duct(str2num(SDelements{1}), str2num(answer{2}), Mach_Frange(1,1))
dimT+1
endstraight_duct()是我在脚本中计算消音器时使用的函数,我有几个函数,我不确定这是否是使用GUI时的工作方式。我只希望你了解我想要达到的目标,并在路上帮助我。因此,我实际上可以为我的脚本创建一个UI :)
发布于 2013-11-29 18:09:37
这假设您已经在GUI的其他地方初始化了storageMatrix,如下所示.
handles.storageMatrix = zeros(2,2,100);
guidata(hObject,handles); % Must call this to save handles so other callbacks can access the new element "storageMatrix"然后在你的第一个按钮的回调..。
% --- Executes on button press in Add_Straightduct.
function Add_Straightduct_Callback(hObject, eventdata, handles)
.
. % Whatever initializations you need to do
.
localStorageMatrix = handles.storageMatrix; %load the storageMatrix being used by other callbacks/functions
.
. % Whatever you need to do to generate the new 2x2 matrix "slice"
.
localStorageMatrix(:,:,end+1) = newSlice; % appends the new slice to the end of the, indexing using colons assumes newSlice is of the same first and second dimension as other slices in localStorageMatrix
handles.storageMatrix = localStorageMatrix; % overwrite the storageMatrix in handles with the contents of the new localStorageMatrix
guidata(hObject,handles); % save the handles struct again now that you've changed it或者,您可以在整个过程中只使用handles.storageMatrix,而不包括localStorageMatrix,但我已经将它包括进来作为重点。
https://stackoverflow.com/questions/20289568
复制相似问题