Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists in Matlab? [closed]

Tags:

matlab

if exist('JaccardDistance', 'file')==1
    load('JaccardDistance');
else
    % Do things
end

The file JaccardDistance is in the same folder where this code is being executed. The problem is that the "else" part is always being executed, which means that it is not recognizing that the file JaccardDistance exists. What am I doing wrong? Thanks in advance.

like image 411
JPC Avatar asked Dec 04 '15 16:12

JPC


People also ask

Which of the following function is used to check if a file exists or not?

The file_exists() function checks whether a file or directory exists.

How do you close a file in MATLAB?

Description. fclose( fileID ) closes an open file. fclose('all') closes all open files.

How do you use exists in MATLAB?

exist name searchType returns the type of name , restricting results to the specified type, searchType . If name of type searchType does not exist, MATLAB returns 0 . A = exist(___) returns the type of name to A .

How do I open a MATLAB file?

On the Home tab, in the File section, click Open , and then select a file to open. You also can double-click the file in the Current Folder browser.


1 Answers

For files you exists will return a 2 not a 1. You should also include the file extension in the check.

if exist('JaccardDistance.m', 'file') == 2

ref matlab forum Or read the manual:

exist name returns the status of name:

  • 0 name does not exist.
  • 1 name is a variable in the workspace.
  • 2 One of the following is true:

    • name exists on your MATLAB® search path as a file with extension .m.
    • name is the name of an ordinary file on your MATLAB search path.
    • name is the full pathname to any file.
like image 186
scrappedcola Avatar answered Sep 29 '22 06:09

scrappedcola