現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Problem working with popupmenu
4 ビュー (過去 30 日間)
古いコメントを表示
Hi guys.
I'm working on GUIDE and i have the next problem:
I have to make a conversor (Decimal, Hexadecimal, octal and bynary)
When i put a number in the edit 1 i have to choose in the "popupmenu1" what value is it (Decimal, Binary, Octal, Hexadecimal) then i choose in the popupmenu2 what value i want convert (Decimal, Binary, Octal, Hexadecimal) and the converted value has to set on the edit2.
mathlab gave me the next message:
"Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)convertidordecantidades('popupmenu2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback."
I hope you guys can help me.
![1.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238410/1.png)
![2.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238411/2.png)
%This is my code%
value = str2double(handles.edittext1.String)
if handles.popupmenu1.Value == 1
if handles.popupmenu2.Value == 1
convertedValue = dec2bin(value);
elseif handles.popupmenu2.Value == 2
elseif handles.popupmenu2.Value == 3
elseif handles.popupmenu2.Value == 4
elseif handles.popupmenu2.Value == 5
end
elseif handles.popupmenu1.Value == 2
if handles.popupmenu2.Value == 1
convertedValue = dec2base(value,8);
elseif handles.popupmenu2.Value == 2
elseif handles.popupmenu2.Value == 3
elseif handles.popupmenu2.Value == 4
elseif handles.popupmenu2.Value == 5
end
elseif handles.popupmenu1.Value == 3
if handles.popupmenu2.Value == 1
convertedValue = dec2base(value,16);
elseif handles.popupmenu2.Value == 2
elseif handles.popupmenu2.Value == 3
elseif handles.popupmenu2.Value == 4
elseif handles.popupmenu2.Value == 5
end
elseif handles.popupmenu1.Value == 3
if handles.popupmenu2.Value == 1
elseif handles.popupmenu2.Value == 2
elseif handles.popupmenu2.Value == 3
elseif handles.popupmenu2.Value == 4
elseif handles.popupmenu2.Value == 5
end
elseif handles.popupmenu1.Value == 5
if handles.popupmenu2.Value == 1
elseif handles.popupmenu2.Value == 2
elseif handles.popupmenu2.Value == 3
elseif handles.popupmenu2.Value == 4
elseif handles.popupmenu2.Value == 5
end
end
handles.edit2.String = sprintf('2.%f',convertedValue)
30 件のコメント
Walter Roberson
2019 年 9 月 15 日
value = str2double(handles.edittext1.String)
That line is only valid if the input is decimal, which is not the case when you are converting from any base other than decimal. You should only be using that line when handles.popupmenu1.Value is 4.
Jhon Rackham
2019 年 9 月 15 日
Hi dear Walter.
What i have to do in this case? Can you help me with that please?
Thanks for the help, master!
Walter Roberson
2019 年 9 月 15 日
handles.edit2.String = sprintf('2.%f',convertedValue)
That line assumes that convertedValue is internal binary format (which would not be a bad thing) and assumes that the output is decimal, but the output should only be decimal if handles.popupmenu2.Value == 4.
You need to re-read
Jhon Rackham
2019 年 9 月 15 日
I was reading your comments dear Walter, but i still don't know how to do it.
So that means my code is useless or i'am the useless? :(
Image Analyst
2019 年 9 月 15 日
編集済み: Image Analyst
2019 年 9 月 15 日
You're setting the converted value in each one of the if statements, right? Not just 3 of them, right?
Jhon Rackham
2019 年 9 月 15 日
編集済み: Jhon Rackham
2019 年 9 月 15 日
Yes Image Analyst i'm setting the converted value in each one of the if statements. But i need set the whatever converted value on the edit 2. I don't know if i'm explaining you good.
pd: I'm using your code you gave me days ago, but i'm still having problems. :(
Image Analyst
2019 年 9 月 15 日
It should work. Post your complete files, both the .fig and .m files. However I'm leaving for the day in a few minutes.
Walter Roberson
2019 年 9 月 15 日
value_in_str = handles.edittext1.String;
switch handles.popupmenu1.Value
case 1
value_in_dec = bin2dec(value_in_str);
case 2
value_in_dec = hex2dec(value_in_str);
end
switch handles.popupmenu2.Value
case 1
value_out_str = dec2bin(value_in_dec);
case 2
value_out_str = dec2hex(value_in_dec);
end
handles.edit2.String = value_out_str;
Image Analyst
2019 年 9 月 15 日
No, you did not have stuff inside each "elseif" block as you claimed. You need to have stuff in each one, like Walter showed. Don't skip any. You can use "if" or switch" - basically the same.
Jhon Rackham
2019 年 9 月 15 日
O was that, I didn't understood then. Si I only have to do that and how I do the rest? Can you please help me fixing my program? ?
Walter Roberson
2019 年 9 月 15 日
What happened when you tried the code I posted in
https://www.mathworks.com/matlabcentral/answers/480482-problem-working-with-popupmenu#comment_746191
and what happened when you examined that and then went back to re-read my hints in https://www.mathworks.com/matlabcentral/answers/479628-how-to-make-a-program-that-conver-a-numer-to-binary-decimal-hexadecimal-and-octal#comment_744171 and used my hints to extend your code to handle other bases?
Jhon Rackham
2019 年 9 月 15 日
It show me errors Mr Roberson.
I notice i'm the useless. Can't do anything. </3
Thats the reazon because i'm asking for favors. :(
Walter Roberson
2019 年 9 月 15 日
It show me errors Mr Roberson.
Would you mind turning your screen by about 7 degrees so that I can read it from here with my binoculars? I'm having terrible difficulty with glare at the moment.
Jhon Rackham
2019 年 9 月 15 日
Look mr Roberson, the same error.
Can i send you my program for you review it please?
![The same error.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238438/The%20same%20error.png)
Jhon Rackham
2019 年 9 月 15 日
I've fixed my problem Mr Roberson.
Please, help me to convert the rest of the values (Octal and Decimal).
I hope you can help me.![fixed.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238440/fixed.png)
![fixed.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238440/fixed.png)
Walter Roberson
2019 年 9 月 15 日
So what did I suggest in https://www.mathworks.com/matlabcentral/answers/479628-how-to-make-a-program-that-conver-a-numer-to-binary-decimal-hexadecimal-and-octal#comment_744171 as the way to handle the other bases ?
Jhon Rackham
2019 年 9 月 15 日
編集済み: Jhon Rackham
2019 年 9 月 15 日
Mr Roberson, please. don't be bad with me. :(
I was reading your comments, but in the code is not the same is guess, i don't know where i have to put the "sscanf() with '%lo' format" for conver octal number.
When you have time for help me Walter?
Jhon Rackham
2019 年 9 月 15 日
I know it is not your obligation to help me, but please, you are a teacher here, it would not take you long to help me. Please I beg you.
Walter Roberson
2019 年 9 月 15 日
>> sscanf('123','%lo')
ans =
uint64
83
>> sprintf('%lo', 83)
ans =
'123'
>> 1*8^2 + 2*8^1 + 3*8^0
ans =
83
String to be converted goes as the first parameter to sscanf(), method of conversion goes as the second parameter to sscanf()
Jhon Rackham
2019 年 9 月 15 日
Awesome Mr Roberson.
My program finally work, thanks a lot for the help, GOD bless you.
Jhon Rackham
2019 年 9 月 15 日
Mr Roberson, are you thereeee?
I forgot to ask you something else.
What can I do to prevent the user from inserting a wrong amount?
For example: if i put this value on the edit1: 123456 and i choose in the popupmenu 1 as binary then i choose on the popupmenu2 i want to convert it as hexadecimal, obvuously it will be an error, because we know 123456 isn't a binary value, maybe for solve this error, we can put a msgbox saying *hey this isn't a binary value*, something like that. But what i have to do for solve this? sorry if i'm bothering you a lot of times.
This is an example:
![je.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238450/je.png)
Walter Roberson
2019 年 9 月 15 日
Preventing the user from entering a number that is invalid for the currently selected base is complicated. It requires checking every keystroke as the user types it in, and handling deletions yourself, and (for obscure reasons) keeping track of what the user typed in by yourself.
It is a lot easier to validate the number at the time the conversion is requested.
- binary: all characters must be members of the set {'0', '1'}
- octal: all characters must be members of the set {'0', '1', '2', '3', '4', '5', '6', '7'}
- decimal: all characters must be members of the set {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
- hex: all characters must be members of the set {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f'}
Walter Roberson
2019 年 9 月 15 日
Consider
ismember('1236', '0':'4')
ismember('1236', ['0':'4', '6':'7'])
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Large Files and Big Data についてさらに検索
タグ
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 (한국어)