function in app give "undefined function for input arguments"
Show older comments
Sample of one of the functions in my code. I made functions for my 2 other apps and they worked fine once I included the correct inputs but I cannot get the correct inputs other than "app" for this code and it gives me a different error than I have seen. Code below:
function PostTestAONButton(app)
if app.MeasurementRangeAONButton.Value == 1
file = dir(fullfile(directory,'*POST-TEST*AON_.txt'));
for i = 1:numel(file)
filepath = fullfile(file(i).folder, file(i).name);
data_in = readmatrix(filepath);
% Grabbing only the frequency and SE values
x = data_in(:,1);
y = data_in(:,3);
% Plotting data based on various options selected
if app.IncludeonLegendCheckBoxAON.Value == 1
if app.MRMarkersButton.Value == 1
plot(x,y,"Color",app.MRColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value), ...
Marker=app.MRMarkerDropDown.Value,DisplayName='MR');
else
plot(x,y,"Color",app.MRColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value), ...
DisplayName='MR');
end
else
if app.MRMarkersButton.Value == 1
plot(x,y,'HandleVisibility','off',"Color",app.MRColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value), ...
Marker=app.MRMarkerDropDown.Value);
else
plot(x,y,'HandleVisibility','off',"Color",app.MRColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value));
end
end
end
else
end
end
7 Comments
Walter Roberson
on 31 Jan 2023
file = dir(fullfile(directory,'*POST-TEST*AON_.txt'));
should that be app.directory ?
Tyler
on 1 Feb 2023
Walter Roberson
on 1 Feb 2023
You have not defined "directory" in that function. Are you expecting that "directory" is a function that is being called without any parameters, and which will return an appropriate directory name? If not then where are you expecting that it will locate a variable named directory? Is the function you posted a nested function with directory being a shared variable?
Tyler
on 7 Feb 2023
Walter Roberson
on 7 Feb 2023
You are expecting that, if necessary, a variable will be resolved to the workspace of the caller of the function.
In matlab the only time that kind of resolution of variables happens is if you define the function being called as a nested function, and the variable is assigned to in the nesting function before the definition of the nested function.
function outer
A = 5;
function C = inner
C = A + B;
end
B = 11;
inner()
end
Variable A is properly nested and is available to inner but variable B is not defined at the point that inner is defined, so it would not be found, and the addition would fail (unless matlab could find a function named B)
@Walter Roberson: "before the definition of the nested function" I believe should be, "before the execution of the nested function"
outer % works
outer2 % fails
function outer
A = 5;
function C = inner % defining inner() here is ok, as long as B is defined before inner() is called
C = A + B;
end
B = 11; % B defined after the definition of inner() but before the execution of inner() works
inner()
end
function outer2
A = 5;
inner() % fails because B is not defined yet
function C = inner
C = A + B;
end
B = 11;
end
Walter Roberson
on 8 Feb 2023
Interesting, my memory had always been that the variable had to be defined before the nested function definition. Either that changed or my memory blipped again. Either of those are real possibilities ;-)
Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!