Using App Designer, reading a numeric edit field and creating a double array from the inputs?

12 次查看(过去 30 天)
I have 3 NumericEditFields and I enter 5700 & 4750 in the first 2 (the 3rd can be blank and thus only have an array with only 2 elements). I then convert them from MegaHertz to Hertz by the following:
if isempty(app.NumericEditField_NotchCenterFreq1.Value) % check to see if the field is empty first, if so, assign an empty string
fc1 = {};
else
fc1 = app.NumericEditField_NotchCenterFreq1.Value;
fc1 = fc1 * 10e6; % Convert to Hertz
end
if isempty(app.NumericEditField_NotchCenterFreq2.Value)
fc2 = {};
else
fc2 = app.NumericEditField_NotchCenterFreq2.Value;
fc2 = fc2 * 10e6; % Convert to Hertz
end
if isempty(app.NumericEditField_NotchCenterFreq3.Value)
fc3 = {};
else
fc3 = app.NumericEditField_NotchCenterFreq3.Value;
fc3 = fc3 * 10e6; % Convert to Hertz
end
% Create an array from entered inputs
notch_fc=[fc1, fc2, fc3];
make_loadset(app, notch_fc);
The notch_fc array is
notch_fc: 1×2 cell array =
[1×4 double] [1×4 double]
But I want it to be a double array (values for example)
notch_fc: 1x2 double =
1.0e+09 *
5.7000 4.7500
How can I accomplish this? I have searched and searched and can not find the answer. Thanks in advance!

采纳的回答

Stephen23
Stephen23 2023-4-22
编辑:Stephen23 2023-4-22
The reason why it is a cell array is because you told MATLAB to make a cell array. Basically you are doing this:
fc1 = 1;
fc2 = pi;
fc3 = {}; % if this is a cell array...
[fc1,fc2,fc3] % then so is this.
ans = 1×2 cell array
{[1]} {[3.1416]}
If any input to the concatenation operator is a cell array, then the output will be a cell array. This is explained here:
Ergo, if you do not want a cell array output, do not provide cell arrays as the inputs.
"How can I accomplish this? I have searched and searched and can not find the answer."
If you want a numeric vector output then you need to provide numeric inputs. Note that numeric arrays cannot have "holes" in them, i.e. every element must contain some value. Some solutions:
  • output may change size: use empty numeric [].
  • output must remain same size: use NaN.
Lets try them both, right now:
fc3 = NaN; % numeric NaN
[fc1,fc2,fc3]
ans = 1×3
1.0000 3.1416 NaN
or
fc3 = []; % numeric empty
[fc1,fc2,fc3]
ans = 1×2
1.0000 3.1416

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by