how to put values in a table from a function in matlab gui

1 view (last 30 days)
i have a table which contains two columns and five rows..and first column is to display some measures and 2nd one is for the values of that measures. the values are taken from a function and the values changes with each execution. I want to set the values in table to the function returning values each time when i run the function by clicking a pushbutton named 'evaluate'. is it possible to do this in matlab gui.. ??

Answers (1)

Geoff Hayes
Geoff Hayes on 8 Sep 2014
Jeena - you can update the table whenever you press the pushbutton named evaluate. If we assume that your uitable widget is named/tagged as uitable1, then in the callback to the pushbutton we can do
function evaluate_Callback(hObject, eventdata, handles)
% call your external function that returns some values needed to populate the table
% EXAMPLE: create a 5x2 matrix of random integers
values = randi(103,5,2);
% convert the values matrix to a cell array
valuesAsCell = mat2cell(values);
% update the table
set(handles.uitable1,'Data',valuesAsCell{:});
In the above example, since I don't have your external function, I just make a call to randi to create a 5x2 matrix of random integers (from 1 to 103). The code then converts this matrix to a cell array/matrix. It will have the same 5x2 dimensions but will be in a format that is valid for the uitable, through the set function call.
Note that with the above code, each time we push the evaluate button, a new set of integers appears in the table.
You can just replace values with that data from your function (or rather, update the 5x2 values matrix with your own data) and it should work. Try it, and see what happens!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!