extract number out of the file title for further usage

5 Ansichten (letzte 30 Tage)
eFKa
eFKa am 28 Nov. 2013
Bearbeitet: eFKa am 28 Nov. 2013
hi everyone,
i have many files inside a folder. The files have names like 12E.dat, 13.dat, ...100.dat i use the following code to load them
files = dir('*.dat');
for k = 1:numel(files)
D = load(files(k).name);
tof = D(:,2)*1000;
...
end
I want to creat an array like E = [12, 13, ..., 100] taken out of the title of the file. Is their a way to realize this?
thanks a lot in advance

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 28 Nov. 2013
files = dir('*.dat');
names = {files.name} ; % put the names in a cell array of strings
names = strrep(names,'.dat','') ; % remove extension
Names is now a cell array of strings. If they are all numbers, you can convert them using str2double
values = str2double(names)

Weitere Antworten (2)

Image Analyst
Image Analyst am 28 Nov. 2013
Did you try sscanf()?

eFKa
eFKa am 28 Nov. 2013
Thanks to both of you guys. For my purpose suggestion of Jos worked very well.
Have a nice evening.
  4 Kommentare
Image Analyst
Image Analyst am 28 Nov. 2013
Very good point (that I overlooked). Sometimes things like that will slip by and you'll only realize it when you get an angry email from a user about how your code does not work. Like they say in software development "It's hard to make code idiotproof because idiots are so ingenious." But perhaps the 12E was just a typo - who knows? But my first assumption is that the user meant what he said.
eFKa
eFKa am 28 Nov. 2013
Bearbeitet: eFKa am 28 Nov. 2013
my files are like 7E.dat to XXE.dat, E indicating energy in Electron-volts. Using the following worked smoothly for me.
names = strrep(names,'E.dat','') ;
scanf() also seems to be interesting because of its versatility. Thanks for the insight once again

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by