jeudi 3 mars 2016

using try to set the condition of a if in MATLAB

I use try to avoid a Index exceeds matrix dimensions. error while going through a list of files. It does what I expect from it:

>> try strcmp(listFiles(6).name(1:9), 'file_name') , catch false, end

ans =

     1

>> try strcmp(listFiles(5).name(1:9), 'file_name')  , catch false, end

ans =

     0

However, if I want to use it inside a if as a condition as follow:

path = 'folder';
listFiles = dir(path);

for i = 1:length(listFiles)
    if (try strcmp(listFiles(i).name(1:9), 'file_name')  , catch false, end)
        [...]
    end
end

it return the following error invalid used of a reserved word.

Is there a method to force it inside or I have to use a independent variable?

Aucun commentaire:

Enregistrer un commentaire