Error with my CODE and I don't know why.
조회 수: 13 (최근 30일)
이전 댓글 표시
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
R1EditFieldLabel matlab.ui.control.Label
R1EditField matlab.ui.control.EditField
R2EditFieldLabel matlab.ui.control.Label
R2EditField matlab.ui.control.EditField
R3EditFieldLabel matlab.ui.control.Label
R3EditField matlab.ui.control.EditField
ResistorsLabel matlab.ui.control.Label
V1EditFieldLabel matlab.ui.control.Label
V1EditField matlab.ui.control.EditField
V2EditFieldLabel matlab.ui.control.Label
V2EditField matlab.ui.control.EditField
CalculateButton matlab.ui.control.Button
I1EditFieldLabel matlab.ui.control.Label
I1EditField matlab.ui.control.EditField
I2EditFieldLabel matlab.ui.control.Label
I2EditField matlab.ui.control.EditField
I3EditFieldLabel matlab.ui.control.Label
I3EditField matlab.ui.control.EditField
VoltagesourcesLabel matlab.ui.control.Label
OutputcurrentsALabel matlab.ui.control.Label
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
R1 = app.R1EditField.Value;
R2 = app.R2EditField.Value;
R3 = app.R3EditField.Value;
V1 = app.V1EditField.Value;
V2 = app.V2EditField.Value;
syms I1 I2 I3;
eqn1 = I1*R1 + I2*R2 == V2 - V1;
eqn2 = I2*R2 + I3*R3 == V2;
eqn3 = I1 + I3 - I2 == 0;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [I1, I2, I3]);
X = linsolve(A,B);
app.I1EditField.Value = double(X(1));
app.I2EditField.Value = double(X(2));
app.I3EditField.Value = double(X(3));
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create R1EditFieldLabel
app.R1EditFieldLabel = uilabel(app.UIFigure);
app.R1EditFieldLabel.HorizontalAlignment = 'right';
app.R1EditFieldLabel.Position = [46 329 25 22];
app.R1EditFieldLabel.Text = 'R1';
% Create R1EditField
app.R1EditField = uieditfield(app.UIFigure, 'text');
app.R1EditField.Position = [86 329 100 22];
% Create R2EditFieldLabel
app.R2EditFieldLabel = uilabel(app.UIFigure);
app.R2EditFieldLabel.HorizontalAlignment = 'right';
app.R2EditFieldLabel.Position = [53 270 25 22];
app.R2EditFieldLabel.Text = 'R2';
% Create R2EditField
app.R2EditField = uieditfield(app.UIFigure, 'text');
app.R2EditField.Position = [93 270 100 22];
% Create R3EditFieldLabel
app.R3EditFieldLabel = uilabel(app.UIFigure);
app.R3EditFieldLabel.HorizontalAlignment = 'right';
app.R3EditFieldLabel.Position = [54 208 25 22];
app.R3EditFieldLabel.Text = 'R3';
% Create R3EditField
app.R3EditField = uieditfield(app.UIFigure, 'text');
app.R3EditField.Position = [94 208 100 22];
% Create ResistorsLabel
app.ResistorsLabel = uilabel(app.UIFigure);
app.ResistorsLabel.Position = [105 375 56 22];
app.ResistorsLabel.Text = 'Resistors';
% Create V1EditFieldLabel
app.V1EditFieldLabel = uilabel(app.UIFigure);
app.V1EditFieldLabel.HorizontalAlignment = 'right';
app.V1EditFieldLabel.Position = [326 316 25 22];
app.V1EditFieldLabel.Text = 'V1';
% Create V1EditField
app.V1EditField = uieditfield(app.UIFigure, 'text');
app.V1EditField.Position = [366 316 100 22];
% Create V2EditFieldLabel
app.V2EditFieldLabel = uilabel(app.UIFigure);
app.V2EditFieldLabel.HorizontalAlignment = 'right';
app.V2EditFieldLabel.Position = [331 270 25 22];
app.V2EditFieldLabel.Text = 'V2';
% Create V2EditField
app.V2EditField = uieditfield(app.UIFigure, 'text');
app.V2EditField.Position = [371 270 100 22];
% Create CalculateButton
app.CalculateButton = uibutton(app.UIFigure, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.Position = [227 144 100 22];
app.CalculateButton.Text = 'Calculate';
% Create I1EditFieldLabel
app.I1EditFieldLabel = uilabel(app.UIFigure);
app.I1EditFieldLabel.HorizontalAlignment = 'right';
app.I1EditFieldLabel.Position = [31 67 25 22];
app.I1EditFieldLabel.Text = 'I1';
% Create I1EditField
app.I1EditField = uieditfield(app.UIFigure, 'text');
app.I1EditField.Position = [70 67 100 22];
% Create I2EditFieldLabel
app.I2EditFieldLabel = uilabel(app.UIFigure);
app.I2EditFieldLabel.HorizontalAlignment = 'right';
app.I2EditFieldLabel.Position = [213 67 25 22];
app.I2EditFieldLabel.Text = 'I2';
% Create I2EditField
app.I2EditField = uieditfield(app.UIFigure, 'text');
app.I2EditField.Position = [253 67 100 22];
% Create I3EditFieldLabel
app.I3EditFieldLabel = uilabel(app.UIFigure);
app.I3EditFieldLabel.HorizontalAlignment = 'right';
app.I3EditFieldLabel.Position = [425 67 25 22];
app.I3EditFieldLabel.Text = 'I3';
% Create I3EditField
app.I3EditField = uieditfield(app.UIFigure, 'text');
app.I3EditField.Position = [465 67 100 22];
% Create VoltagesourcesLabel
app.VoltagesourcesLabel = uilabel(app.UIFigure);
app.VoltagesourcesLabel.Position = [396 354 91 22];
app.VoltagesourcesLabel.Text = 'Voltage sources';
% Create OutputcurrentsALabel
app.OutputcurrentsALabel = uilabel(app.UIFigure);
app.OutputcurrentsALabel.Position = [224 105 108 22];
app.OutputcurrentsALabel.Text = 'Output currents (A)';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Exercise4_Marcelo
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Note: this is what indicates "Error using matlab.ui.control.EditField/set.Value (line 98)
'Value' must be a character vector or a string scalar."
댓글 수: 1
Mohsin Zubair
2021년 7월 8일
Try using num2str(Your value) also it would be better to share your entire app because sharing all this code like this we can't find the error ourselves.
답변 (2개)
Shiva Kalyan Diwakaruni
2021년 5월 13일
Hi,
the error message indicates that the Edit Field's Value must be a character vector or string. MATLAB does not automatically convert a variable to a char, but you can do so by using the "char" function. To do this, you need to change the line:
app.seriesEditField.Value = yy;
to:
app.seriesEditField.Value = char(yy);
If you intend for your variable to be displayed as a number, you may be better served by creating a Numeric Edit Field in your app and converting to double instead of char. You can find the Numeric Edit Field in the Component Library just above the Text Edit Field. Your line of code would then become:
app.seriesEditField.Value = double(yy);
Hope it helps.
댓글 수: 0
VBBV
2024년 5월 10일
편집: VBBV
2024년 5월 10일
You can modify the following lines in your code. i.e. set the uieditfield property to numeric instead of text
app.I1EditField = uieditfield(app.UIFigure, 'numeric'); % set to numeric
app.I2EditField = uieditfield(app.UIFigure, 'numeric');
app.I3EditField = uieditfield(app.UIFigure, 'numeric');
app.R1EditField = uieditfield(app.UIFigure, 'numeric');
app.R2EditField = uieditfield(app.UIFigure, 'numeric');
app.R3EditField = uieditfield(app.UIFigure, 'numeric');
app.V1EditField = uieditfield(app.UIFigure, 'numeric');
app.V2EditField = uieditfield(app.UIFigure, 'numeric');
댓글 수: 0
참고 항목
카테고리
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!