Search code examples
matlabmatlab-figurematlab-uitablematlab-app-designer

Set limits for values in the cells of an uitable


I am creating a GUI using Matlab's App Designer (2019b). One of the nice features for NumericEditField is that you can define value limits so that users can't enter a value outside the desired range. For example, the following would limit the edit field values between -100 and 100.

app.numericEditField1.Limits = [-100 100];

I also have a uitable object in my GUI - is it possible to set value limits for cells in the data table, like with the edit fields? I didn't see a property that was obviously equivalent. My best thought for a workaround is to edit the CellEditCallback to manually check the values every time one is changed.

Below is a sample app that has a value edit field with limits, and a regular uitable. I would like the to put value limits on certain columns of the table as well.

Sample code

classdef sampleLimitedValApp < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
    UIFigure                        matlab.ui.Figure
    LimitedEditValueEditFieldLabel  matlab.ui.control.Label
    LimitedEditValueEditField       matlab.ui.control.NumericEditField
    UITable                         matlab.ui.control.Table
end

% Callbacks that handle component events
methods (Access = private)

    % Code that executes after component creation
    function startupFcn(app)
        app.UITable.Data = zeros(3,4);
    end
end

% Component initialization
methods (Access = private)

    % Create UIFigure and components
    function createComponents(app)

        % Create UIFigure and hide until all components are created
        app.UIFigure = uifigure('Visible', 'off');
        app.UIFigure.Position = [100 100 383 331];
        app.UIFigure.Name = 'UI Figure';

        % Create LimitedEditValueEditFieldLabel
        app.LimitedEditValueEditFieldLabel = uilabel(app.UIFigure);
        app.LimitedEditValueEditFieldLabel.HorizontalAlignment = 'right';
        app.LimitedEditValueEditFieldLabel.Position = [31 280 101 22];
        app.LimitedEditValueEditFieldLabel.Text = 'Limited Edit Value';

        % Create LimitedEditValueEditField
        app.LimitedEditValueEditField = uieditfield(app.UIFigure, 'numeric');
        app.LimitedEditValueEditField.Limits = [-100 100];
        app.LimitedEditValueEditField.Position = [147 280 100 22];

        % Create UITable
        app.UITable = uitable(app.UIFigure);
        app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
        app.UITable.RowName = {''};
        app.UITable.ColumnEditable = true;
        app.UITable.Position = [31 67 302 185];

        % Show the figure after all components are created
        app.UIFigure.Visible = 'on';
    end
end

% App creation and deletion
methods (Access = public)

    % Construct app
    function app = sampleLimitedValApp

        % Create UIFigure and components
        createComponents(app)

        % Register the app with App Designer
        registerApp(app, app.UIFigure)

        % Execute the startup function
        runStartupFcn(app, @startupFcn)

        if nargout == 0
            clear app
        end
    end

    % Code that executes before app deletion
    function delete(app)

        % Delete UIFigure when app is deleted
        delete(app.UIFigure)
    end
end
end

Solution

  • Your idea of using the CellEditCallback was the right one. I have to admit, that I am not really an expert in creating and using classes in Matlab and always create my GUIs from scratch without using the AppDesigner, thats why I don't know if there is maybe a better organization of functions and methods possible.

    However, the following does what you want:

    % Component initialization
    methods (Access = private)
    
        % Create UIFigure and components
        function createComponents(app)
    
            % original code
    
            % added code
            app.UITable.CellEditCallback = @limitCellVal;
            function limitCellVal(src,evt)
                CellLimits = [-100 100];
                idx = evt.Indices; % indices of selected cell
                belowLowerLimit = src.Data(idx(1),idx(2)) < CellLimits(1);
                aboveUpperLimit = src.Data(idx(1),idx(2)) > CellLimits(2);
                if belowLowerLimit, src.Data(idx(1),idx(2)) = CellLimits(1); end
                if aboveUpperLimit, src.Data(idx(1),idx(2)) = CellLimits(2); end
            end
    
        end
    
    end
    

    If you want to edit multiple cells at once, than a little tweaking of the callback function is necessary, but I think you can manage that.