Field text- number

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

2 commentaires

Aldin
Aldin le 14 Mar 2012
Do you work in MATLAB GUI?
Aldin
Aldin le 14 Mar 2012
I don't know but i quess this is what you want:
set(handles.edit1,'String',num2str(4.5));

Connectez-vous pour commenter.

 Réponse acceptée

Aldin
Aldin le 14 Mar 2012

1 vote

I have tested my code:
set(handles.edit1,'String',num2str(4.5));
It's correct.

25 commentaires

john
john le 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"
Aldin
Aldin le 14 Mar 2012
Please paste your code here it's confused to me to understand.
john
john le 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
Aldin le 14 Mar 2012
Your code is for action for Button, right?
john
john le 14 Mar 2012
Yes :)
john
john le 14 Mar 2012
and also in condition "if" I need add condition for number with "," for example "3,57987"
Aldin
Aldin le 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
Aldin le 14 Mar 2012
"3.57987" is in MATLAB not correct. The correct way is: 3.57987
john
john le 14 Mar 2012
Sorry, but num = get(handles.edit1,'String');
if str2double(num) < 0
doesn't work ...
john
john le 14 Mar 2012
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.
john
john le 14 Mar 2012
In the number, or string, can I find symbol " , " or " . "?
Aldin
Aldin le 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
Walter Roberson le 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.
Aldin
Aldin le 14 Mar 2012
"357987E-5" is string, right?
Walter Roberson
Walter Roberson le 14 Mar 2012
>> class(357987E-5)
ans =
double
Aldin
Aldin le 14 Mar 2012
But when yout type in text file "357987E-5" it becomes string, because:
get(handles.edit1,'String');
Walter Roberson
Walter Roberson le 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.
Aldin
Aldin le 14 Mar 2012
it represent an String. The condition is only for '.' ans ','
Aldin
Aldin le 14 Mar 2012
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
Walter Roberson le 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.
Aldin
Aldin le 14 Mar 2012
Yes, you have right Do you agree it's a little confused question form Vansac?
john
john le 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
john le 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
Aldin le 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
john
john le 20 Mar 2012
Great idea, find(string==',') is very helpfull, thank yuo again

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by