이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Field text- number
조회 수: 1 (최근 30일)
이전 댓글 표시
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일
Sorry, but num = get(handles.edit1,'String');
if str2double(num) < 0
doesn't work ...
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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 System Commands에 대해 자세히 알아보기
태그
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 (한국어)