Namespace not recognized when used in a function called from an app
I have an app that calls a function in an external file. The function references a class in a namespace directory (beginning with a +). I set up the paths in the app to include the namespace directory’s parent folder and not the namespace directory. When the app calls the function, the function generates a MATLAB:UndefinedFunction error that it cannot locate the namespace.class. When I debug I stop at the problematic line and can access the namespace as normal in the debug command line. I can execute the code of the line where the error occurs, but if I click continue in the debugger the code fails.
Simplified exemple code:
MyProject/
+MyNameSpace/
MyClass.m
MyApp.mlapp
MyFunction.m
startup.m
MyClass.m:
classdef MyClass
properties
value = 1;
end
end
MyFunction.m:
function value = MyFunction()
value = MyNameSpace.MyClass.value;
end
startup.m:
function startup
% Determine where your m-file’s folder is.
folder = fileparts(which(mfilename));
% Add that folder plus all subfolders to the path.
addpath(folder);
end
Then inside the MyApp.mlapp I use the startupFcn method that is called by the constructor to call startup.m:
function startupFcn(app)
startup
end
And a button callback to call MyFunction:
% Button pushed function: RunButton
function RunButtonPushed(app, event)
value = MyFunction();
end
The returned error states:
"Unrecognized function or variable ‘MyNameSpace’."I have an app that calls a function in an external file. The function references a class in a namespace directory (beginning with a +). I set up the paths in the app to include the namespace directory’s parent folder and not the namespace directory. When the app calls the function, the function generates a MATLAB:UndefinedFunction error that it cannot locate the namespace.class. When I debug I stop at the problematic line and can access the namespace as normal in the debug command line. I can execute the code of the line where the error occurs, but if I click continue in the debugger the code fails.
Simplified exemple code:
MyProject/
+MyNameSpace/
MyClass.m
MyApp.mlapp
MyFunction.m
startup.m
MyClass.m:
classdef MyClass
properties
value = 1;
end
end
MyFunction.m:
function value = MyFunction()
value = MyNameSpace.MyClass.value;
end
startup.m:
function startup
% Determine where your m-file’s folder is.
folder = fileparts(which(mfilename));
% Add that folder plus all subfolders to the path.
addpath(folder);
end
Then inside the MyApp.mlapp I use the startupFcn method that is called by the constructor to call startup.m:
function startupFcn(app)
startup
end
And a button callback to call MyFunction:
% Button pushed function: RunButton
function RunButtonPushed(app, event)
value = MyFunction();
end
The returned error states:
"Unrecognized function or variable ‘MyNameSpace’." I have an app that calls a function in an external file. The function references a class in a namespace directory (beginning with a +). I set up the paths in the app to include the namespace directory’s parent folder and not the namespace directory. When the app calls the function, the function generates a MATLAB:UndefinedFunction error that it cannot locate the namespace.class. When I debug I stop at the problematic line and can access the namespace as normal in the debug command line. I can execute the code of the line where the error occurs, but if I click continue in the debugger the code fails.
Simplified exemple code:
MyProject/
+MyNameSpace/
MyClass.m
MyApp.mlapp
MyFunction.m
startup.m
MyClass.m:
classdef MyClass
properties
value = 1;
end
end
MyFunction.m:
function value = MyFunction()
value = MyNameSpace.MyClass.value;
end
startup.m:
function startup
% Determine where your m-file’s folder is.
folder = fileparts(which(mfilename));
% Add that folder plus all subfolders to the path.
addpath(folder);
end
Then inside the MyApp.mlapp I use the startupFcn method that is called by the constructor to call startup.m:
function startupFcn(app)
startup
end
And a button callback to call MyFunction:
% Button pushed function: RunButton
function RunButtonPushed(app, event)
value = MyFunction();
end
The returned error states:
"Unrecognized function or variable ‘MyNameSpace’." app designer, namespaces, classes MATLAB Answers — New Questions









