Lucas
Lucas

Reputation: 257

Accept a specific type of file Matlab

I have a GUI using a Browe Button to search a file :

function Browse(app, event)
     FileName,FilePath ]= uigetfile();
     ExPath = fullfile(FilePath, FileName);
     app.FileTextArea.Value = ExPath;  
end

And i save the file Path in a Text Area. I have another button that start a matlab script with the file path as parameter and so i would like to accept only a certain type of file (.ctm which is my own type of file) if possible like this :

if file is .ctm
    do something
else 
    print('a .ctm file is needed')

Thanks for helping

Upvotes: 0

Views: 88

Answers (1)

Michał Machnicki
Michał Machnicki

Reputation: 2887

There are two things you can do:

  1. Display only the files with a certain extension with uigetfile()

    [fileName, dataDir] = uigetfile('*.ctm', 'Select a *.ctm file', yourDefaultPth);
    
  2. Verify that selected file has a .ctm extension

    [data.dir,data.fileName,data.ext] = fileparts(fullfile(dataDir, fileName)); % dataDir and fileName from pt. 1
    
    if strcmp(data.ext, '.ctm')
        % do something
    else 
        print('a .ctm file is needed')
    end
    

Keep in mind that neither of the two will verify that the content of the file is the one you're expecting and if someone will manually modify extension of the file, your program will most likely crash. It's good for a start but if you want to do a more reliable check, you should verify that the content of the file is correct, not its extension.

Upvotes: 1

Related Questions