Why am I keep getting "Unrecognized function or variable 'title'. " and "Variable might be used before it is defined" for dynamic title in app designer

2 次查看(过去 30 天)
This is the code for the title:
title(app.UIAxes,['From ' char(cell2mat(app.PastMonthName)) ' of ' num2str(app.PastYear) ' to ' char(cell2mat(app.CurrentMonthName)) ' of ' num2str(app.CurrentYear) ' '], FontSize=20)
char(cell2mat(app.PastMonthName)) -------> results in "March"
num2str(app.PastYear) ------------------------------------> results in 2021
char(cell2mat(app.CurrentMonthName)) ------> results in "October"
num2str(app.CurrentYear) --------------------------------> results in 2022
This is the function Plotting(app) that the title() is in:
function Plotting(app)
if ConditionA
%code here etc etc
if ConditionA && ConditionPizza
%code here etc etc
if ConditionWood && ConditionA && ConditionPizza
%code here etc etc
if ConditionCelticsWillLose && ConditionWood && ConditionA && ConditionPizza
%code here etc etc
title(app.UIAxes,['From ' char(cell2mat(app.PastMonthName)) ' of ' num2str(app.PastYear) ' to ' char(cell2mat(app.CurrentMonthName)) ' of ' num2str(app.CurrentYear) ' '], FontSize=20)
end
end
end
end
end
I keep getting this error: "Variable might be used before it is defined"
The Command Window shoes a little bit more clarification: "Unrecognized function or variable 'title'."
Unrecognized function or variable 'title'.
Error in CSat_App/Plotting (line 201)
title(app.UIAxes,['% of Units Able to Successfully Transmit Data from ' char(cell2mat(app.PastMonthNameBU1)) ' ' num2str(app.PastMonthYearBU1) ' to ' char(cell2mat(app.CurrMonthNameBU1)) ' ' num2str(app.CurrMonthYearBU1) ' '],FontSize=20)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in CSat_App/ListBoxValueChanged (line 2642)
Plotting(app);
^^^^^^^^^^^^^
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 60)
newCallback = @(source, event)executeCallback(ams, ...
^^^^^^^^^^^^^^^^^^^^^^^^
Error while evaluating ListBox PrivateValueChangedFcn.
My ListBox PrivateValueChangedFcn is:
function ListBoxValueChanged(app, event)
app.ListBox.Items = vertcat({'All Customers'}, app.uniqueNummyNums);
app.valueFromListBox = app.ListBox.Value;
Plotting(app);
end
Community, do you have any suggestions as to how to resolve this?
I have no variable assigned to title(), so I am not understanding how the title() can be used before it is defined.
The dynamic variables, PastMonthName, CurrentMonthName, PastYear, and CurrentYear, are all defined early on in the startupFcn(app) way before Plotting(app) is stated. Plotting(app) is the last line of code in the startupFcn() as well as the ListBoxValueChanged(app,event) functions.
I am thinking that the 'event' input in ListBoxValueChanged(app,event) may be significant in some way, but I cannot see how.

采纳的回答

Walter Roberson
Walter Roberson 2024-12-13
Very likely, at some later point in the code you have an assignment to title . MATLAB "looks forward" and sees the eventual assignment, so it decides that "title" must be a variable for the purposes of the code, but at that point in the code the variable has not yet been defined.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by