function in app give "undefined function for input arguments"

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

file = dir(fullfile(directory,'*POST-TEST*AON_.txt'));
should that be app.directory ?
I didn't need to make it app.directory on my other code and that line is the same in both the others (similar codes for different applications)
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?
Here is the start of the code.
if app.SingleFolderButton.Value == 1
% Enter the directory to search
directory = uigetdir('*',"Select Folder With Files To Be Processed");
% List all items in the folder
fileList = dir(directory);
% Delete the subfolders from the list (i.e. only keep files)
fileList(vertcat(fileList.isdir)) = [];
% Set lamp color to yellow for in progress
app.Lamp.Color = 'Yellow';
% Uses folder as title of plot
[ParentFolderPath] = fullfile(directory);
[~, ParentFolderName] = fileparts(ParentFolderPath);
% Loop through each file, copy it and give new extension: .txt
for i = 1:numel(fileList)
file = fullfile(directory, fileList(i).name);
[tempDir, tempFile] = fileparts(file);
status = copyfile(file, fullfile(tempDir, [tempFile, '.txt']));
end
figure
hold all
PostTestAONButton(app);
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
ans = 16
outer2 % fails
Unrecognized function or variable 'B'.

Error in solution>outer2/inner (line 16)
C = A + B;

Error in solution>outer2 (line 14)
inner() % fails because B is not defined yet
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
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 ;-)

Sign in to comment.

Answers (0)

Categories

Products

Release

R2021b

Asked:

on 31 Jan 2023

Commented:

on 8 Feb 2023

Community Treasure Hunt

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

Start Hunting!