App designer Problem : error matlab.ui.control.internal.model.AbstractNumericComponent/set.Value (Line 111 ) 'Value' have to be double scalar
3 次查看(过去 30 天)
显示 更早的评论
% 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
0 个评论
回答(2 个)
Walter Roberson
2021-1-25
编辑:Walter Roberson
2021-1-25
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(...))
0 个评论
Cris LaPierre
2021-1-25
编辑:Cris LaPierre
2021-1-25
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 个评论
Walter Roberson
2021-1-25
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.
Cris LaPierre
2021-1-25
True. Oddly enough, there was another post today with the exact same error. See my answer to that one.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!