Mana V
Mana V

Reputation: 77

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?

Upvotes: 3

Views: 801

Answers (2)

Robert Seifert
Robert Seifert

Reputation: 25232

excaza was a little faster in writing basically the same answer like me. As it looks a slightly different, I'll post it anyway.

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

The "user behaviour" I imitated with the input function. The basic idea is to update your table from within test(). evalin you can use, if you don't want to pass parameters to test(), though passing the handle of the uitable directly is certainly the better option.

If you are working on a serious GUI project I highly recommend you reading this answer.

Upvotes: 2

sco1
sco1

Reputation: 12214

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.

Upvotes: 2

Related Questions