我想保存通过3D矩阵中的GUI按钮输入的矩阵。例如:我点击按钮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
end
straight_duct()
是我在计算消声器时在脚本中使用的函数,我有几个这样的函数,我不确定这是否是使用GUI时的工作方式。我希望你能理解我想要实现的目标,并在我的路上帮助我。所以我实际上可以为我的脚本创建一个UI:)
答案 0 :(得分:1)
这假设你已经在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
,但我已将其包含在内以供强调。