Error in app designer
15 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (2 件)
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,
- When you create an app from app designer, there is a default code generated based on the components which is non-editable.
- 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!
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
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 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!