Easy way for positioning data into uitable – Matlab

Matlab offers a simple way to display tabular data inside ower GUI’s. In this post I will show the simplest way to fill data inside the uitable. Using this way, it is possible to skip rows and columns to make the resulting table prettier

    %Create the main figure
    figure_object = figure;
    
    %Create the uitable with row and column names
    row_names = {'Subject 1', 'Subject 2', 'Subject 3' , 'Subject 4', '', 'Total'};
    col_names = {'Name', '', 'Variable 1', 'Variable 2', 'Variable 3', 'Variable 4'};
    
    table_object = uitable('Parent', f,...
        'Rowname', row_names,...
        'ColumnName', col_names,...
        'Position', [20, 20, 550, 150]);

    %Initiate table data structure
    nrows = 6;
    ncols = 6
    table_data = cell(nrows, ncols);

    %Fill the cells with subject names
    table_data(1:4) = {'Paul', 'Joseph', 'Mary', 'Carl'};

    %Fill the cells with some random values and skip one row
    %and one column
    table_data(1:6, 3) = {'10.23', '09.12', '12.67', '11.04', '', '43.06'};
    table_data(1:6, 4) = {'110.42', '67.09', '82.61', '101.04', '', '361.16'};
    table_data(1:6, 5) = {'0.45', '1.23', '0.54', '3.11', '', '5.33'};
    table_data(1:6, 6) = {'Yes', 'No', 'No', 'Yes', '', ''}

Resulting GUI

Bildschirmfoto 2015-07-05 um 12.45.07 vorm.

Leave a comment