finding files in a directory

조회 수: 25 (최근 30일)
joseph Frank
joseph Frank 2011년 8월 15일
Hi,
I have text files that start with a1 , a2,a3b,a4b .. I was using the following loop to find the files in the directory that start with a1,a2,a3b,and ab4: for k=1:4 x={'1';'2';'3b';'4b';...} Files=dir(['a',x(k),'.txt.*']); end it didn't work because ??? Error using ==> dir Function is not defined for 'cell' inputs.
I know that it works for a1 and a2 if i substitute x(k)by num2str(1) and num2str(2) but what about a3b and a4b files?

채택된 답변

Walter Roberson
Walter Roberson 2011년 8월 15일
Files = dir(sprintf('a%s.txt.*' x{k}));
Are you sure, by the way, that you have .txt followed by another extension such that a final * would be appropriate? Are you sure it isn't
Files = dir(sprintf('a%s*.txt', x{k}));
that you would prefer, with the wildcard between the initial letters and the .txt ?

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 8월 15일
Files=dir(['a',x{k},'.txt.*'])
When you reference cell array, you need to use {}, not ().
  댓글 수: 1
Image Analyst
Image Analyst 2011년 8월 15일
That's one thing you learn with cell arrays - you usually pick the wrong ones to start with. So if () doesn't work, try {}, and if {} doesn't work, try (). It's an easy and quick debugging procedure that I think everybody uses fairly often when dealing with cell arrays.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by