Undefined function or variable 'isempty'.

5 Ansichten (letzte 30 Tage)
KG
KG am 17 Jan. 2023
Kommentiert: Walter Roberson am 17 Jan. 2023
Hello all, I'm using version 2016a and have encountered some behaviour I've never seen before. I'm trying to check an excel spreadsheet to see if an entry already exists for a given serial number before adding a new entry. The problem section is:
recordsTable = readtable(FILE_ADDRESS, 'Sheet', systemSheet, 'Range', 'A:I'); % This reads in the data in a table
existingRecord = find(recordsTable.SerialNumbers == projectNum); % This finds any entries which match a given numerical value
if isempty(existingRecord) % This returns the error: Undefined function or variable 'isempty'.
...
end
I've used breakpoints to check and existingRecord is empty when isempty is called on it. Indeed, if I place a breakpoint on the problem line and then call isempty(existingRecord) from the command line instead of letting the script continue it works perfectly and returns a 1 as expected. Running each line individually from the command line also works and returns a 1 as expected. It is only when used as part of a script that MATLAB refuses to recognize isempty, despite it being a default function.
I have checked and isempty is on the MATLAB path as it should be. It is included as a built-in function in version 2016a, and my license is current. I've triple checked my spelling of all function and variable names and have spent several hours googling what could be wrong to no avail. No part of my code alters the path, and I'm not trying to use isempty or any variation thereof as a variable name. Any advice would be appreciated as at this point I have no idea why isempty seems to be unusable in a script, despite several hours of googling and reading documentation.
  7 Kommentare
KG
KG am 17 Jan. 2023
Walter, I don't know exactly why this worked, but I think you have inadvertently helped me solve it. I didn't have isempty assigned as a variable anywhere, but while I ctrl-f'd to double check I noticed that in a completely different if statement it was written as
isempty{existingRecord}
instead of
isempty(existingRecord)
Switching to parentheses instead of curly braces did something and now it works again. My (admittedly very limited) understanding is that culy braces only tell matlab that it's a cell array so I have no idea why that made it so that MATLAB couldn't recognize the function in a completely different part of the script, but it's working as intended now. Thanks!
Walter Roberson
Walter Roberson am 17 Jan. 2023
When matlab saw {} in the code it deduced that isempty must be a variable, and so it does not search the path looking for it as a function.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by