Matlab GUI - Edit text only allow numbers and '.'
古いコメントを表示
I would like to create an edit text that only allows to introduce numbers and '.' . If another letter is pressed it should write nothing.
Using KeyPressFcn I am able to detect the letter pressed and compare to the valid ones but I do not know what to do in order to not to write the character if it is not valid.
Any idea?
採用された回答
その他の回答 (1 件)
Jan
2011 年 7 月 1 日
This behaviour will annoy the user. The limitation of the keyboard feels sticky. Better let the user write anything and check the input in the callback function only, e.g. by:
function myNumberCheck(ObjH, EventData)
S = get(ObjH, 'String');
% Exclude characters, which are accepted by sscanf:
S(ismember(S, '-+eEgG')) = ' ';
% Convert to one number and back to a string:
S2 = sprintf('%g', sscanf(S, '%g', 1));
set(ObjH, 'String', S2);
% Perhaps a small warning in WARNDLG or inside the GUI:
if ~all(ismember(S, '.1234567890'))
...
end
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!