Error in app designer

조회 수: 15 (최근 30일)
Vahid Abolhasannejad
Vahid Abolhasannejad 2020년 4월 20일
댓글: Zayad 2023년 12월 12일
I have faced an error in my code like it says
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
on the following statement:
% Component initialization
methods (Access = private)
It's a solid statement at the end of code and I cannot change it at all. Does anybody know how to fix this problem? since it doesn't allow me to run the program.
Thank you.
Vahid.
  댓글 수: 8
Vahid Abolhasannejad
Vahid Abolhasannejad 2021년 4월 7일
Hello Walter, thank you for your comment. That part I mentioned was among the defauls of the code that could not be changed. So the problem was solved. Thanks again for your help.
Zayad
Zayad 2023년 12월 12일
how did you solve it?

댓글을 달려면 로그인하십시오.

답변 (2개)

Ganesh Regoti
Ganesh Regoti 2020년 4월 28일
Hi Vahid,
By the error statement, I would say it might be a syntactical error. Go through the code to check if it is properly formatted and ended. Regarding the solid statements,
  1. When you create an app from app designer, there is a default code generated based on the components which is non-editable.
  2. When you add callbacks, properties, methods, an editable area is created with declarations where you can edit/add your code.
Here are the links you refer to for more information
Hope this helps!
  댓글 수: 1
Vahid Abolhasannejad
Vahid Abolhasannejad 2021년 4월 7일
Hello Ganesh, thank you for your comment. That part I mentioned was among the defauls of the code that could not be changed. So the problem was solved. Thanks again for your help.

댓글을 달려면 로그인하십시오.


Justi Kertusha
Justi Kertusha 2023년 11월 30일
편집: Walter Roberson 2023년 12월 3일
% LightBehaviorApp.m
classdef LightBehaviorApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access=public)
UIFigure matlab.ui.Figure
Material1EditField matlab.ui.control.NumericEditField
Material2EditField matlab.ui.control.NumericEditField
IncidentAngleEditField matlab.ui.control.NumericEditField
CalculateButton matlab.ui.control.Button
TrajectoryAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods(Access=private)
% Code that executes after component creation
function startupFcn(app)
% Set default values for the indices of refraction
app.Material1EditField.Value = 1.5;
app.Material2EditField.Value = 1.0;
app.IncidentAngleEditField.Value = 45;
end
% Button pushed function
function CalculateButtonPushed(app, ~)
% Get user inputs
n1 = app.Material1EditField.Value;
n2 = app.Material2EditField.Value;
incidentAngle = app.IncidentAngleEditField.Value;
% Calculate refraction angle using Snell's Law
refractedAngle = asind((n1/n2) * sind(incidentAngle));
% Plot the trajectory of light within each medium
cla(app.TrajectoryAxes);
hold(app.TrajectoryAxes, 'on');
plot(app.TrajectoryAxes, [0, cosd(incidentAngle)], [0, sind(incidentAngle)]);
plot(app.TrajectoryAxes, [0, cosd(refractedAngle)], [-sind(incidentAngle), -sind(incidentAngle)]);
hold(app.TrajectoryAxes, 'off');
axis(app.TrajectoryAxes, 'equal');
title(app.TrajectoryAxes, 'Light Trajectory');
legend(app.TrajectoryAxes, {'Incident Ray', 'Refracted Ray'});
end
end
% App creation and deletion
methods (Access=public)
% Construct app
function app = LightBehaviorApp
% Create UIFigure and components
createComponents(app);
% Register the app with App Designer
registerApp(app, app.UIFigure);
% Execute the startup function
runStartupFcn(app, @startupFcn);
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
% Function to create and configure UI components
methods (Access=private)
function createComponents(app)
% Create UIFigure and set its properties
app.UIFigure = matlab.ui.Figure('Visible', 'off');
app.UIFigure.Position = [100, 100, 600, 400];
app.UIFigure.Name = 'LightBehaviorApp';
% Create Material1EditField component
app.Material1EditField = matlab.ui.control.NumericEditField(app.UIFigure);
app.Material1EditField.Position = [100, 300, 100, 30];
app.Material1EditField.String = '1.5';
app.Material1EditField.Label = 'Material 1 Refractive Index';
% Create Material2EditField component
app.Material2EditField = matlab.ui.control.NumericEditField(app.UIFigure);
app.Material2EditField.Position = [100, 250, 100, 30];
app.Material2EditField.String = '1.0';
app.Material2EditField.Label = 'Material 2 Refractive Index';
% Create IncidentAngleEditField component
app.IncidentAngleEditField = matlab.ui.control.NumericEditField(app.UIFigure);
app.IncidentAngleEditField.Position = [100, 200, 100, 30];
app.IncidentAngleEditField.String = '45';
app.IncidentAngleEditField.Label = 'Incident Angle';
% Create CalculateButton component
app.CalculateButton = matlab.ui.control.Button(app.UIFigure);
app.CalculateButton.Position = [250, 200, 100, 30];
app.CalculateButton.Text = 'Calculate';
app.CalculateButton.ButtonPushedFcn = @app.CalculateButtonPushed;
% Create TrajectoryAxes component
app.TrajectoryAxes = matlab.ui.control.UIAxes(app.UIFigure);
app.TrajectoryAxes.Position = [400, 100, 150, 200];
end
end
end
This is given me an error in line 55
Error in LightBehaviorApp (line 55)
createComponents(app);
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 12월 3일
app.Material1EditField = matlab.ui.control.NumericEditField(app.UIFigure);
When you call matlab.ui.control.NumericEditField directly, then any parameters you pass must be paired, property/value form. When you call it directly, it does not accept a figure positionally.
You should be calling uieditfield(app.UIFigure,"numeric") instead.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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!

Translated by