How to make a program that conver a numer to binary, decimal, hexadecimal and octal.
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi guys, can someone help me with my project please?
I need a program that convert a number to decimal, binary, hexadecimal or octal value.
I'am working on GUI and i have on my GUIDE the next things:
2 popupmenu
2 edittext
Well, i was thinking i could do a simple conversor (like google conversor).
And the edittext1 i enter the number to convert, then i choosee on the popupmenu1 what value is it(it can be decimal, binary, hexadecimal or octal value)
after that, i choose on the popupmenu2 what value i wanna convert and that value will be set on the edittext2.
I'am a beginner here,guys i hope you can help me with the code, please, help me with a code with the native variables(like edittext, popupmenu, etc).
Thanks ☺
댓글 수: 1
답변 (2개)
Walter Roberson
2019년 9월 9일
If you generate a uicontrol popup or listbox in GUIDE, and you examine the comments for the code it generates for the callbacks, it tells you how to find out which entry was selected:
idx = get(hObject, 'Value');
This will be empty of nothing has been selected, and otherwise will be a positive integer 1 to the number of entries in the String property, indicating which of the entries has been selected.
You can find out which string was selected by using:
all_entries = get(hObject, 'String');
selected_string = all_entries{idx};
This will be a character vector that you can proceed to process with strcmp() or a switch/case expression:
switch selected_string
case 'Binary'
...
case 'Hexadecimal'
...
end
The above presumes that you are within the Callback for either the source or target popup. You can get access to the other of the two popups by referencing it by tag instead of referencing hObject, such as
src_idx = get(handles.source_base, 'Value');
targ_idx = get(handles.target_base, 'Value');
댓글 수: 2
Walter Roberson
2019년 9월 10일
In the switch case for the source base, case 'Binary' should use bin2dec() to convert the input string to decimal. 'Hexadecimal' should use sscanf() with '%lx' format. 'Decimal' should use sscanf() with '%ld' format. 'Octal' should use sscanf() with '%lo' format'. In all four cases, the output should be a decimal number.
Then you have a second switch case, this time for the target base. case 'Binary' should use dec2bin() to convert the decimal to binary. 'Hexadecimal' should use sprintf() with '%lx' format to convert the decimal to hex. 'Decimal' should use sprintf() with '%ld' format. 'Octal' should use sprintf() with '%lo' format.
Take the resulting string and set the String property of the target field to it.
Note: I recommend that you make the target field a uicontrol style 'text' rather than a uicontrol style 'edit' as you do not want the user to be able to edit the output.
Ted Shultz
2019년 9월 9일
There are built in matlab funtions that will do this conversion for you.
Check out:
etc.
Good luck!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!