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

3 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-9-14
"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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-9-14
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by