You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Field text- number
1 view (last 30 days)
Show older comments
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
Accepted Answer
Aldin
on 14 Mar 2012
I have tested my code:
set(handles.edit1,'String',num2str(4.5));
It's correct.
25 Comments
john
on 14 Mar 2012
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
on 14 Mar 2012
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!');
Aldin
on 14 Mar 2012
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
Aldin
on 14 Mar 2012
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
on 14 Mar 2012
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.
Walter Roberson
on 14 Mar 2012
>> class(357987E-5)
ans =
double
Walter Roberson
on 14 Mar 2012
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.
Walter Roberson
on 14 Mar 2012
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
on 17 Mar 2012
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
on 20 Mar 2012
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
on 20 Mar 2012
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
More Answers (0)
See Also
Categories
Find more on System Commands in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)