Reading From Different Directories in a for loop (fopen from different directories)

2 views (last 30 days)
Hi,
My problem is,
I can not read the file from different directory/folder My file name is same all the time . It is " kEG1____1.gid ".(It is atext file)
C:\......Case_Set_1.135\simulation.dir\kEG1____1.gid
C:\......Case_Set_1.136\simulation.dir\kEG1____1.gid
C:\......Case_Set_1.137\simulation.dir\kEG1____1.gid
...
I can read and process the text file individually with the code that Mr/Ms Per Isakson wrote from this forum.It uses fopen(..9) for opening the file.The link is here. http://www.mathworks.com/matlabcentral/answers/37437-textscan-reading-after-a-specified-term-or-a-row-and-writing-matrix )
But the problem is all the files located in different directories.
C:\......Case_Set_1.135\simulation.dir\kEG1____1.gid
C:\......Case_Set_1.136\simulation.dir\kEG1____1.gid
C:\......Case_Set_1.137\simulation.dir\kEG1____1.gid
...
I tried to obtain directory names with sprintf but it gives an error.
for i=136:137
fullFileName=sprintf('C:\Users\KARTAL\Dropbox\CaseDenemeler\BWF25PRE_2000\boost\135-143set.Case_Set_1.1%d\simulation.dir\kEG1____1.gid',i)
end
But it gives " Invalid escape sequence appears in format string. See help sprintf for valid escape sequences." which I think it is an backslash forwardslash error..
But I could not solve the problem.
anyone can help me about this situation.
Best Regards.

Accepted Answer

per isakson
per isakson on 6 May 2012
There is a function in the FEX, Recursive directory listing - Enhanced RDIR, which I guess will help you out.
The call will be something like
sad = rdir( 'root_something\**\kEG1*1.gid' )
maybe
sad = rdir( 'c:\Users\KARTAL\Dropbox\CaseDenemeler\BWF25PRE_2000\boost\135-143set.Case_Set_1.13*\**\kEG1*1.gid' )
There are two rdir in the FEX. I intended to link to Recursive directory listing. That is the one that has become part of my Matlab. However, I guess either one will do the job.
  1 Comment
Orkun OZENER
Orkun OZENER on 8 May 2012
Dear Per Isakson,
Thansk for you answer again. It is really an enhanced and advanced function.
Thanks for your note. I downloaded to my folder. And I am trying to understand the function. Thanks for your answer and for your provious answer about reading the file. I am just working on code. If you dont mind May be in the future I will ask some questions again. best Regards.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 6 May 2012
Simple solution: use forward slashes instead of backslashes. Even Windows handles them just fine:
fullFileName=sprintf('C:/Users/KARTAL/Dropbox/CaseDenemeler/BWF25PRE_2000/boost/135-143set.Case_Set_1.1%d/simulation.dir/kEG1____1.gid',i)
Another tip: Use genpath() to get a list of all subfolders. Perhaps this may come in useful in the event that you don't know all the subfolder names in advance.

Walter Roberson
Walter Roberson on 6 May 2012
fullFileName=sprintf('%s%d%s', 'C:\Users\KARTAL\Dropbox\CaseDenemeler\BWF25PRE_2000\boost\135-143set.Case_Set_1.1', i, '\simulation.dir\kEG1____1.gid')
end
  1 Comment
Orkun OZENER
Orkun OZENER on 8 May 2012
Dear Mr.Roberson,
Thanks for your answer and for your approach.
You script also solved my problem.
Best regards.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!