App designer Problem : error matlab.ui.​control.in​ternal.mod​el.Abstrac​tNumericCo​mponent/se​t.Value (Line 111 ) 'Value' have to be double scalar

6 views (last 30 days)
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
numEditFieldLabel matlab.ui.control.Label
numEditField matlab.ui.control.NumericEditField
resEditFieldLabel matlab.ui.control.Label
resEditField matlab.ui.control.NumericEditField
diffButton matlab.ui.control.StateButton
end
properties (Access = private)
x % Description
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: diffButton
function diffButtonValueChanged(app, event)
fx = (app.x)^2 + app.x;
difffx = diff(fx,app.x,1);
app.resEditField.Value = subs(difffx,app.x,app.numEditField.Value);%%%%%%% this is the place where i got error
end
end

Answers (2)

Walter Roberson
Walter Roberson on 25 Jan 2021
Edited: Walter Roberson on 25 Jan 2021
app.x is not initialized, so it defaults to empty.
fx = (app.x)^2 + app.x;
value^2 is only valid if the value is empty or scalar or a square matrix. We cannot tell which from that alone.
difffx = diff(fx,app.x,1);
The three-parameter version of diff requires that the second parameter be scalar or empty. If app.x is numeric scalar then fx is numeric scalar, and the second parameter would be the number of differences to take; with fx being scalar, if that does not outright fail, the diff is going to return empty unless app.x happens to not be a positive integer, in which case error. If app.x is empty then it would be treated as 1 but since fx would be empty in that case, you would get empty anyhow. So if app.x is numeric then the diff is either going to error (not empty or positive integer) or return empty.
app.resEditField.Value = subs(difffx,app.x,app.numEditField.Value);%%%%%%%this is the place where i got error
If app.x is empty or numeric then if we got here then subs(empty) is going to be empty and that would not be scalar and you need a scalar at that point.
So... what if app.x just happens to store a scalar symbolic variable? Then the diff() would work and produce a scalar symbolic expression, and the subs would probably work and produce a scalar symbolic expression.
So what is the problem in that case? This: you cannot set that kind of field to a symbolic expression, it is the wrong data type. You need to convert the result to the subs() into a numeric datatype. Like double(subs(...))

Cris LaPierre
Cris LaPierre on 25 Jan 2021
Edited: Cris LaPierre on 25 Jan 2021
The result of subs is still symbolic. You need to convert it to a double. You can use the double function for that.
app.resEditField.Value = double(subs(difffx,app.x,app.numEditField.Value));
  3 Comments
Walter Roberson
Walter Roberson on 25 Jan 2021
This is probably the problem, but we cannot prove it with the information given; my Answer shows that the result of the subs could be empty. The innards of the test would have to be examined to see whether it fails first for not being a scalar or fails first for not being numeric.

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!