Field text- number
3 次查看(过去 30 天)
显示 更早的评论
john
2012-3-14
Hi,
if I write integer number like 4 into field text, thens it is ok.
But when I write floating number like 4.5 into field text, then write error...
How can I do this?
Thanks
采纳的回答
Aldin
2012-3-14
I have tested my code:
set(handles.edit1,'String',num2str(4.5));
It's correct.
25 个评论
john
2012-3-14
Sorry, my mistake....yes in GUI.
But if I write integer number into field text, I want to show for example " This is ok" .
But when I write floating number into field text, then I want to show for example " error-you inserted floating number, please insert integer number"
john
2012-3-14
v=str2double(get(handles.edit5,'String'));
if ((v<=0)||(isnan(v))) % I need add condition for floating number
set(handles.text5, 'String','error-you inserted floating number, please insert integer number!');
else
set(handles.text5, 'String','This is ok!');
john
2012-3-14
and also in condition "if" I need add condition for number with "," for example "3,57987"
Aldin
2012-3-14
Put this as your Button action:
num = get(handles.edit1,'String');
if str2double(num) < 0
set(handles.edit2,'String','error-you inserted floating number, please insert integer number!');
else
set(handles.edit2,'String','This is OK');
end
john
2012-3-14
and I want to add condition for number with coma 3,4342 and with dot 4.4324
and when I write any string, then I want to show " This is ok" .
So, string and integer number is OK, but number with coma and dot are not OK.
Aldin
2012-3-14
Copy/Paste this code it works:
string = get(handles.edit1,'String');
if length(string) >= 2
if strcmp(string(2),'.') | strcmp(string(2),',')
set(handles.edit2,'String','error-you inserted floating number, please insert integer number!');
else
set(handles.edit2,'String','This is OK');
end
else
set(handles.edit2,'String','This is OK');
end
Walter Roberson
2012-3-14
357987E-5
is a floating point number that has no "." and no ","
NaN does not have "." or ",". inf and -inf do not either.
if ~all(ismember(string, '0123456789'))
%not a non-negative number
end
You need to decide about negative numbers, and you need to decide about 0.
Aldin
2012-3-14
But when yout type in text file "357987E-5" it becomes string, because:
get(handles.edit1,'String');
Walter Roberson
2012-3-14
The code above almost all has str2double() calls.
Anyhow, if you put '357987E-5' in to the String field of handles.edit1 and run your code noted above, then because no character in it is a '.' or a ',' your code would say it was OK, which is not correct as 357987E-5 does not represent an integer.
Aldin
2012-3-14
We can use "isstr" function when we type isstr('357987E-5') = 1 it means that is an string or isstr(23.3234) = 0. Right?
Walter Roberson
2012-3-14
ischar() is the recommended replacement for isstr()
You seem to have lost track of the initial question. "if I write integer number like 4 into field text, thens it is ok. But when I write floating number like 4.5 into field text, then write error..."
If the user writes 357987E-5 in to the field then that is a floating point number and so should, according to the original criteria, create an error. But if the user writes 357987E+5 in to the field then that is an integer and so should not create an error.
When you get() the String of the edit field, you are always going to get a char array as output, whether that char array contains '357987E-5' or '23.3234' . At the point before you do any str2double(), you just have a string, no matter what that string contains.
Vansac, change your code from
v=str2double(get(handles.edit5,'String'));
if ((v<=0)||(isnan(v)))
to
v = str2double(get(handles.edit5,'String'));
if v<=0 || isnan(v) || ~isfinite(v) || v ~= fix(v)
This also catches infinities.
john
2012-3-17
Hi guys,
I made combination of your codes:
v=str2double(get(handles.edit5,'String'));
string = get(handles.edit5,'String');
%if length(string) >= 2
%a=length(string)
if v<=0 || isnan(v) || ~isfinite(v) || v ~= fix(v) || strcmp(string(1),'.') || strcmp(string(1),',') || strcmp(string(2),',') || strcmp(string(3),',') .
.
.
if I write for example fhsfuis...error...it works
if I write for , ...error...it works......command strcmp(string(1),',')
if I write for . ...error...it works
if I write for 6.5757 ...error...it works
if I write for 6,676 ...error...it works ...command strcmp(string(2),',')
if I write for 43,7 ...error...it works...command strcmp(string(3),',')
but if I write for 564,87 ...thens is not error...this doesn't works....is possible to make automatically create command strcmp(string(1),',')???
Walter, command if v<=0 || isnan(v) || ~isfinite(v) || v ~= fix(v) doesn't work for numbers with ","...I don't know why...therefore I made the above combination
john
2012-3-20
Hi, can you help me please? I have always problem with coma ","
If I write number only with one decimal place for example: 5,5 or 34,4 or 434,9 or 4533,5 or 45353,3 then it works,.
..
..
..but if I write for example 5,55 or 5,555 or 43,4534 then it doesn't work.
.
.
.
here is my code
if length(string) >= 2
a=length(string)-1
if ((v<=0) || (isnan(v)) || (~isfinite(v)) || (v ~= fix(v)) || strcmp(string(1),'.') || strcmp(string(a),','))
set(handles.text3,'String','Error');
end;
end;
Aldin
2012-3-20
Here is on maybe better solution: use *find* function.
For example if you have string like this:
>>string = '453,45434';
you can use *find* function you have to check if there in string exist comma: find(string==',') the result will be 4.
Now, if you have string like this: >>string = '4534434' (without comma) the result for _find(string==',')_ will be Empty matrix: 1-by-0. I hope my advice will be helpful
更多回答(0 个)
另请参阅
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)