現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Field text- number
1 回表示 (過去 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 日
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)