【问题标题】:Saving GUI data from matlab in a 3D matrix将 matlab 中的 GUI 数据保存在 3D 矩阵中
【发布时间】: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


【解决方案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,但我将其包括在内是为了强调。

【讨论】:

  • 非常感谢您的回复!我明天试试。我真的是在找人向我解释它的死胡同:)
  • 这实际上向我展示了如何使用手柄。正确!它很有用。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
相关资源
最近更新 更多