Search code examples
matlabuser-interfacematlab-figurematlab-guidematlab-uitable

How to update a uitable after creation from other functions?


I created a matfile in which I store data that are constantly overwritten by user behavior. This occurs in a function "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

Then, I want to display these data in another function (it is essential to have two separated functions) called "storageTest()". More particularly, storageTest() is a GUI function where I developped an uitable "t". So, I first call the function "test()" and give its output values as data of "t". Here is the code of the interesting part of "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

This code executes only when "m = test()" running is over and displays me a tab in which I can see the final value of "n". However, I want my table to be displayed before and to see my value incrementing according to user's behavior. I have searched on the web to solve my issue, but I cannot find any answer, is it possible to do such a thing?


Solution

  • Assuming I'm interpreting the question correctly, it should be fairly trivial to accomplish this if you initialize your table prior to calling test and then pass the handle to your table for test to update in the while loop:

    For example:

    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
    

    When you run the above, you'll notice the data in your table iterating as expected.