Extract .txt files from a list

조회 수: 1 (최근 30일)
Yewande Oni
Yewande Oni 2015년 6월 9일
댓글: Yewande Oni 2015년 6월 11일
Hi,
I am trying to extract .txt files from a list of files I created into a cell array. From there I want to put them in a directory that feeds into a loop. Can you help please?
Thanks
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 6월 9일
Yes, and notice that the ext will include the '.' so Adam is correct in saying to compare against '.txt' rather than 'txt'
Yewande Oni
Yewande Oni 2015년 6월 10일
Ok, can I apply this to a struct and put them into a folder to create a directory folder?

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

채택된 답변

Walter Roberson
Walter Roberson 2015년 6월 10일
If YourCell is a cell array of strings that represent file names, then
is_txt_file = ~cellfun(@isempty, regexp(YourCell, '\.txt$'));
this will give you a logical vector that you can use such as
txtFileNames = YourCell(is_txt_file);
After that you can process them however you like.
If what you have is a dir() result and you want to pick out the txt files from that, then supposing DirInfo is the directory structure,
YourCell = {DirInfo.name};
is_txt_file = ~cellfun(@isempty, regexp(YourCell, '\.txt$'));
txtDirInfo = DirInfo(is_txt_file);
  댓글 수: 1
Yewande Oni
Yewande Oni 2015년 6월 11일
Perfect! It works now, thanks.

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

추가 답변 (0개)

카테고리

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