find a specific file
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Cedric
am 8 Okt. 2013
Bearbeitet: Cedric
am 8 Okt. 2013
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?
Akzeptierte Antwort
Cedric
am 8 Okt. 2013
Bearbeitet: Cedric
am 8 Okt. 2013
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 Kommentare
Cedric
am 8 Okt. 2013
Bearbeitet: Cedric
am 8 Okt. 2013
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.
Weitere Antworten (1)
Image Analyst
am 8 Okt. 2013
You can use sprintf() to create filenames based on variables:
filename = sprintf('Stresses_AxSym_UP_%d_%d.txt', integer1, integer2);
2 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!