Is there an easy way to display a "table" in Matlab GUI?

I am saving reports from my system in matlab as tables generated by the command table()

. To display these tables I used disp(myTable)

. It was fine when I was only looking at the tables in the shell.

However, now I want to create a GUI that will display these tables along with graphs and other information. I found out that I can display strings in static text GUI elements by doing

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

...

However, I cannot find an easy way to turn the data stored as a table into a table.

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

EDIT: Ok, so I found a way to store the table in a row:

tableString=evalc('disp(table)')

But the result is a disaster and doesn't look like the neatly formatted string I get in the shell.

+3


source to share


1 answer


Here's a kind of hack with uitable , which creates a UI table component in the GUI. "uitable" is built from the elements of an existing table. The following GUI has a button, and after clicking it, the UI table is created and placed inside the GUI at a specific location. Then you can play 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 how it looks after clicking the button:



enter image description here

So, as you can see, the important part is that you are positioning the table intelligently so that it does not appear above other elements of your GUI.

Hope it helps!

+5


source







All Articles