Is there a simple way to display "table" in matlab GUI?

BoZenKhaa picture BoZenKhaa · Apr 17, 2015 · Viewed 8.9k times · Source

I save reports from my system in matlab in form of tables generated by the table() command. To display these tables, I was using the disp(myTable). This was fine when I was viewing the tables only in the shell.

However, now I wish to build a GUI that will display these tables along with plots and other information. I found out I can display strings in the Static Text GUI elements by doing

set(handles.staticText1, 'String', 'My text! Yay!').

However, I can not figure out any simple way of turning data saved as table into a string.

Is there a simple way to display tables in GUI or do I need to manually extract all the columns from table?

EDIT: Ok, so I found a way of saving table into a string:

tableString=evalc('disp(table)')

But the result is a disaster and looks nothing like the neatly formatted string I get in the shell.

Answer

Benoit_11 picture Benoit_11 · Apr 17, 2015

Here is kind of a hack using uitable, which creates a UI table component into a GUI.The "uitable" is built from elements of an existing table. The following GUI has a pushbutton and once you press it, a UI table is created and placed inside the GUI at a specified location. You can then play around with it.

function MakeTableGUI

clear
clc
close all

%// Create figure and uielements
handles.fig = figure('Position',[440 400 500 230]);

handles.DispButton = uicontrol('Style','Pushbutton','Position',[20 70 80 40],'String','Display table','Callback',@DispTableCallback);

%// Create table
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

handles.T = table(Age,Height,Weight,BloodPressure,'RowNames',LastName);

    function DispTableCallback(~,~)

        %// Place table in GUI
        uitable(handles.fig,'Data',handles.T{:,:},'ColumnWidth',{50},'ColumnName',{'Age','Height','Weight','BloodPressure'},...
            'RowName',LastName,'Position',[110 20 300 150]);


    end

end

This is what it looks like after pressing the button:

enter image description here

So as you see, the important part is that you place the table judiciously so that it does not appear over other elements of your GUI.

Hope that helps!