Using GUI Designer to convert integer inputs to exponential form (convert MHz to integer Hertz)
조회 수: 1 (최근 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
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)
채택된 답변
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'
x1n = str2num(x1)
x1d = str2double(x1)
x1n - x1d
x2 = '5700e6'
x2n = str2num(x2)
x2d = str2double(x2)
x2n - x2d
x1d * 1e6 - x2d
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!