Using GUI Designer to convert integer inputs to exponential form (convert MHz to integer Hertz)

3 views (last 30 days)
My orginial inputs in a .m function are in Hertz and then combined in an aray as follows:
x1 = 5700e6
x2 = 4750e6
x3 = 4950e6
x_array = [x1, x2, x3]
In Matlab Designer/GUI where the user can enter values in MHz, how do I input the values as say 5700 MHz and have it convert it to 5700e6?
This is what I have currently, but it doesn't match the orginal above:
x1 = str2double(app.EditField_x1.Value);
x1 = x1 * 10e5;
x_array = [x1, x2, x3];
The 'double' could be the issue.... Should it be str2num or something else? Then can I multiply that value to represent it as a Hertz value (to match the form 5700e06)? I can't seem to get it to match and it is affecting my output.
Thanks in advance!
  2 Comments
Stephen23
Stephen23 on 14 Sep 2023
"how do I input the values as say 5700 MHz and have it convert it to 5700e6?"
num = str2double('5700');
fprintf('%.30f\n', num*1e6, num*10^6, 5700e6)
5700000000.000000000000000000000000000000 5700000000.000000000000000000000000000000 5700000000.000000000000000000000000000000

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Sep 2023
If the user is expected to enter 5700 and that is to represent the same as 5700e6 would, then you should be multiplying by 1e6 instead of by 1e5.
str2double() is better than str2num() in the majority of cases.
format long g
x1 = '5700'
x1 = '5700'
x1n = str2num(x1)
x1n =
5700
x1d = str2double(x1)
x1d =
5700
x1n - x1d
ans =
0
x2 = '5700e6'
x2 = '5700e6'
x2n = str2num(x2)
x2n =
5700000000
x2d = str2double(x2)
x2d =
5700000000
x2n - x2d
ans =
0
x1d * 1e6 - x2d
ans =
0

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!