【发布时间】:2015-11-23 03:19:47
【问题描述】:
我想将通过 GUI 按钮输入的矩阵保存在 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
end
straight_duct() 是我在计算消声器时在脚本中使用的一个函数,我有几个这样的函数,我不确定这是否是使用 GUI 时的工作方式。我只是希望你能了解我想要实现的目标并在我的道路上帮助我。所以我实际上可以为我的脚本制作一个 UI :)
【问题讨论】:
-
考虑使用
handles.storageMatrix = zeros(2,2,100);并确保调用guidata(hObject,handles)以保存对handles结构的更改,然后离开回调函数,一个元素被添加到结构中。也许其他人可以阐明这种方法或setappdata()方法是否更好。
标签: matlab