MATLAB Answers

Dir function gives nothing

5 views (last 30 days)
I am using Dir function to find the specific files that have cell in their names but it retunrs nothing although cell files are existing in that folder. Here is the code:
selpath = uigetdir;
struct = dir([selpath '*cell*.json']);
and here is the sample image of the folder:
1.JPG

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 16 Nov 2019
Concatenating strings to create the filename is the problem.
Use fullfile instead, your code will work fine.
Zeynab Mousavikhamene
Zeynab Mousavikhamene on 16 Nov 2019
@Stephen Cobeldick Thanks a lot. Unfortunately I can not give you thumbs up as it is comment.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Nov 2019
selpath = uigetdir;
if isempty(selpath); return; end %user cancel
dinfo = dir(fullfile(selpath, '*cell*.json'));
uigetdir() does not promise that what it returns will end with a directory separator, so use fullfile()
Using struct as a variable name would interfere with creating structures.

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags