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

1 次查看(过去 30 天)
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.

采纳的回答

per isakson
per isakson 2012-5-6
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 个评论
Orkun OZENER
Orkun OZENER 2012-5-8
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.

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2012-5-6
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 2012-5-6
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

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by