MATLAB Answers

0

How to make a program that conver a numer to binary, decimal, hexadecimal and octal.

Asked by Jhon Rackham on 9 Sep 2019
Latest activity Commented on by Walter Roberson
on 10 Sep 2019
Hi guys, can someone help me with my project please?higuys.png
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 ☺

Tags

2 Answers

Answer by Walter Roberson
on 9 Sep 2019

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 Comments

Hi Mr Roberson, thank you so much for the help.
But I don't know what I have to do in the "switch case" what I have to put into it? Can you help me with the entire code please.
Sorry if i'am bothering you asking for the entire code, but I can't do it alone, i'am a begginer here, I hope you can help me.
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.

Sign in to comment.


Answer by Ted Shultz
on 9 Sep 2019

There are built in matlab funtions that will do this conversion for you.
Check out:
etc.
Good luck!

  0 Comments

Sign in to comment.