How to validate user input in a UITable between three values?

I just one the user gives numbers either 0.5, 0.7 or 1
function damage
% Create table array
t = readtable('Country.xls');
vars = {'Damage','G','k1','k2','k3','k4'};
t = t(1:32,vars);
% Create UI figure
fig = uifigure;
% Create table UI component
uit = uitable(fig);
uit.Data = t;
% uit.ColumnSortable = true;
uit.ColumnEditable = [false false true true true true];
uit.CellEditCallback = @k1CheckCB;
end
I did another function like this, but I'm not sure how it is supossed to work with a triple conditional
function k1CheckCB(t)
if (t.k1 == 0.5 || t.k1 == 0.7 || t.k1 == 1.0)
tableData = uit.Data;
tableData{t.k1} = t.PreviousData;
uit.Data = tableData;
warning('Extent values are only 0.5, 0.7 and 1.0')
end
end

 採用された回答

Yongjian Feng
Yongjian Feng 2021 年 8 月 3 日

0 投票

Why not a drop down selection of three values?

7 件のコメント

DulceEien
DulceEien 2021 年 8 月 3 日
could I use categorical?
% Drop down
extent = uicontrol('style','popup','string',{'0.5';'0.8';'1.0'});
t.k1 = extent;
like this I don't know how to assign values
Yongjian Feng
Yongjian Feng 2021 年 8 月 3 日
編集済み: Yongjian Feng 2021 年 8 月 3 日
A simple way is to only modify your callback function to something like this:
function k1CheckCB(hObj, t)
if ~ismember(t.NewData, [0.5 0.7 1.0])
% no good.
warning('Exten must be 0.5, 0.7, or 1.0');
% reset
data = hObj.Data;
data(t.Indices(1), t.Indices(2)) = t.PreviousData;
hObj.Data = data;
end
end
DulceEien
DulceEien 2021 年 8 月 3 日
thank you for your help
DulceEien
DulceEien 2021 年 8 月 3 日
sorry to ask again if I look only in the third column is the Previous Data correct? (I got an error in the index array)
if (t.Indices(3) == 3 && ...
~ismember(t.NewData, [0.5 0.7 1.0]))
% no good.
warning('Exten must be 0.5, 0.7, or 1.0');
% reset
data = hObject.Data;
data{t.Indices(1), t.Indices(2), t.Indices(3)} = t.PreviousData;
hObject.Data = data;
end
Yongjian Feng
Yongjian Feng 2021 年 8 月 3 日
編集済み: Yongjian Feng 2021 年 8 月 3 日
Oh, k1 is the 3rd column, right? Then you are looking at t.Indices(2) = 3. If you only want to check the k1 column and don't need to worry about the other columns:
function k1CheckCB(hObj, t)
if t.Indices(2) == 3 % only care about the k1 column
if ~ismember(t.NewData, [0.5 0.7 1.0])
% no good.
warning('Exten must be 0.5, 0.7, or 1.0');
% reset
data = hObj.Data;
data(t.Indices(1), t.Indices(2)) = t.PreviousData;
hObj.Data = data;
end
end
end
DulceEien
DulceEien 2021 年 8 月 3 日
編集済み: DulceEien 2021 年 8 月 3 日
I understand, it is because I wanted to limit only to those values for that k1 column (values either 0.5, 0.7 or 1), but I saw that ~ismember for a row values, before as you mentioned I wanted to assign a drop down bottom but I got also a mistake
something like this:
extent = categorical(t.k1,{'0.5','0.7','1.0'});
t.k1 = extent;
DulceEien
DulceEien 2021 年 8 月 3 日
@Yongjian Feng thank you so much again, it is working

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by