3

我创建了一个 matfile,其中存储了不断被用户行为覆盖的数据。这发生在函数“test()”中。

n=1
while n < 5 
    myVal = double(Test704(1, 780, -1)) %Returns the user's behavior
    if myVal == 1
        n = n + 1 %"n" is the overwritten variable in the matfile
    end

    save('testSave1.mat') %The matfile
    m = matfile('testSave1.mat')
end

然后,我想在另一个名为“storageTest()”的函数(必须有两个独立的函数)中显示这些数据。更具体地说, storageTest() 是一个 GUI 函数,我在其中开发了一个合适的“t”。因此,我首先调用函数“test()”并将其输出值作为“t”的数据。下面是“storageTest”有趣部分的代码:

m = test()
    d = [m.n]
    t = uitable('Data',d, ...
        'ColumnWidth',{50}, ...
        'Position',[100 100 461 146]);
    t.Position(3) = t.Extent(3);
    t.Position(4) = t.Extent(4);

    drawnow

此代码仅在“m = test()”运行结束时执行,并向我显示一个选项卡,在该选项卡中我可以看到“n”的最终值。但是,我希望我的表格之前显示并看到我的值根据用户的行为而增加。我已经在网上搜索以解决我的问题,但我找不到任何答案,是否有可能做这样的事情?

4

2 回答 2

2

假设我正确地解释了这个问题,如果你在调用之前初始化你的表test,然后将句柄传递给你的表以便testwhile循环中更新,那么完成这个应该是相当简单的:

例如:

function testGUI
% Initialize table
t = uitable('ColumnWidth',{50}, 'Position',[100 100 461 146]);
test(t)

function test(t)
n = 1;
while n < 5
    n = n + 1;
    t.Data = n;
    pause(0.25); % Since we're just incrementing a number, a slight so we can actually see the change
end

当您运行上述程序时,您会注意到表中的数据按预期迭代。

于 2015-08-21T17:08:36.453 回答
2

excaza 写得快一点,基本上和我一样的答案。由于它看起来略有不同,我还是会发布它。

function storagetest()
    close all
    f = figure;
    data = [1];
    t = uitable(f,'Data',data,'ColumnWidth',{50});
    test()
end

function test()
    % handle uitable
    t = evalin('caller','t')

    n = 1;
    while n < 5
        newVal = input('Enter a number:');
        data = get(t,'Data');
        set(t,'Data', [data; newVal]);
        n = n + 1;
    end

end

我用该input功能模仿的“用户行为”。基本思想是从内部更新您的表test()evalin如果您不想将参数传递给 ,则可以使用,test()尽管直接传递 uitable 的句柄肯定是更好的选择。

如果您正在处理一个严肃的 GUI 项目,我强烈建议您阅读这个答案

于 2015-08-21T17:11:47.653 回答