find a specific file

1 次查看(过去 30 天)
Nicolas
Nicolas 2013-10-8
评论: Cedric 2013-10-8
Hello,
I have different data on a similar numerical model stored in text with similar but different names.
What I want to know is if it possible to find a file base on its name Basically I open these files in my code:
Stresses-UP-7_5-22_5-0MPa.txt
Stresses-UP-8-22-14MPa.txt
Within my code, I would like to find a specific value stored in another text file names:
Stresses_AxSym_UP_7-5_22-5.txt
Stresses_AxSym_UP_8_22.txt
both text files correspond to the same models having dimensions: 1) 7.5 and 22.5; and 2) 8 and 22.
the text file names are different, in one the names are separated by underscore and the other by the minus sign. Would it be possible to code something to find the Stress_AxSym text file based on the information of Stress_UP text file?
thank you
  2 个评论
Cedric
Cedric 2013-10-8
编辑:Cedric 2013-10-8
So you know the first file name and you want to generate the corresponding second file name based on the first? Or are you just asking how to generate all file names based on 7.5, 22.5, 8, 22? In any case, what happens to the 'OMPa' and '14MPa' parts of the first names? Are they discarded?
Nicolas
Nicolas 2013-10-8
I'd like to use the information in the name of file 1 to find file 2

请先登录,再进行评论。

采纳的回答

Cedric
Cedric 2013-10-8
编辑:Cedric 2013-10-8
You could use the following function:
function fName_out = convertFilename( fName_in )
match = regexp( fName_in, '[\d_]+', 'match' ) ;
dims = strrep( match(1:2), '_', '-' ) ;
fName_out = sprintf( 'Stresses_AxSym_UP_%s_%s.txt', dims{1}, dims{2} ) ;
end
With that ..
>> convertFilename( 'Stresses-UP-7_5-22_5-0MPa.txt' )
ans =
Stresses_AxSym_UP_7-5_22-5.txt
>> convertFilename( 'Stresses-UP-8-22-14MPa.txt' )
ans =
Stresses_AxSym_UP_8_22.txt
  2 个评论
Nicolas
Nicolas 2013-10-8
That's great. There are lot of functions I didn't know there. I'll play with that for now. Thanks a lot.
Cedric
Cedric 2013-10-8
编辑:Cedric 2013-10-8
You're welcome.
The call to REGEXP returns sub-strings like '7_5', '8', etc. The advantage over SSCANF is that it is more flexible and we can manage cases where there is a decimal part (7.5) as well as integer cases (8) in one expression. SSCANF doesn't have this flexibility, yet it works much faster than REGEXP when you can use it. If time were critical, I'd build a solution based on SSCANF (as mentioned by ImageAnalyst), but here it isn't, as reading/processing files is orders of magnitude slower than the REGEXP call.
The first two elements of the cell array outputted by REGEXP are the two numbers with the '_' in place of the decimal point. Using STRREP, we replace the underscore (if present) in both with a dash. The generates for example the cell array {'7-5', '22-5'} for the first file name.
We finally print these two strings at relevant locations (where '%s' are) in the pattern describing your output file name format.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2013-10-8
You can use sprintf() to create filenames based on variables:
filename = sprintf('Stresses_AxSym_UP_%d_%d.txt', integer1, integer2);
  2 个评论
Nicolas
Nicolas 2013-10-8
nice, thanks. Would it be possible to extract the %d in order to match them in another file.
For example: file 1 named -7_5-22_5- corresponds to the file named _7-5_22_5; since both correspond the the same model with characteristics (7.5 and 22.5) expect that they are written differently.
Image Analyst
Image Analyst 2013-10-8
Sure. Use sscanf().

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by