2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MATLAB で UITabのセル単位で編集状態を変える方法

2
Last updated at Posted at 2026-05-24
classdef UITableMixedCellExample < matlab.apps.AppBase

    properties (Access = public)
        UIFigure matlab.ui.Figure
        UITable  matlab.ui.control.Table
    end

    methods (Access = private)

        function startupFcn(app)

            % 初期データ
            data = {
                'A', 'AUTO';
                'B', 10;
                'C', 'OFF'
                };

            % UITable作成
            app.UITable = uitable(app.UIFigure);
            app.UITable.Position = [20 20 350 150];
            app.UITable.Data = data;

            % 列設定
            app.UITable.ColumnName = {'ID','Value'};
            app.UITable.ColumnEditable = [false true];

            % 重要:列は char にしておく(混在対策)
            app.UITable.ColumnFormat = {'char','char'};

            %app.UITable.CellEditCallbackに cellEdit を登録しておくこと
        end

        function cellEdit(app,event)

            % 選択セルの [row col]
            row = event.Indices(1);
            col = event.Indices(2);

            if col ~= 2
                return;
            end

            data = app.UITable.Data;
            newVal = event.NewData;

            %===========================
            % (1,2) → Dropdown制御
            %===========================
            if row == 1

                allowed = {'AUTO','MANUAL','OFF'};

                if ~ismember(string(newVal), allowed)

                    uialert(app.UIFigure,...
                        '1行目はDropdown値のみ',...
                        'Error');

                    data{row,col} = event.PreviousData;
                    app.UITable.Data = data;
                    return;
                end
            end

            %===========================
            % (2,2) → 数値制御
            %===========================
            if row == 2

                val = str2double(newVal);

                if isnan(val)

                    uialert(app.UIFigure,...
                        '2行目は数値のみ入力',...
                        'Error');

                    data{row,col} = event.PreviousData;
                    app.UITable.Data = data;
                    return;
                end

                data{row,col} = val;
            end

            %===========================
            % 反映
            %===========================
            app.UITable.Data = data;

        end
    end

    methods (Access = public)

        function app = UITableMixedCellExample

            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 400 200];

            startupFcn(app);

        end

        function UITableCellSelection(app, event)

            indices = event.Indices;
            if isempty(indices)
                return;
            end

            % 選択セルの [row col]
            row = indices(1,1);
            col = indices(1,2);
            if col == 2
                if row == 1
                    app.UITable.ColumnFormat = {'char', {'AUTO','MANUAL','OFF'}};
                end
                if row == 2
                    app.UITable.ColumnFormat = {'char', 'numeric'};
                end
            end
        end            
    end
end
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?