It depends on how fixed your filename is. If your filename is in the string filename
, then you can use regexp
to extract parts of your filename, like so:
filename = 'Ev_An_OM2_l5_5000.txt'; %or whatever
parts = regexp(filename,'[^_]+_[^_]+_([^_]+)_[^_]+_([^\.]+)\.txt','tokens');
This will give you parts{1}=='OM2'
and parts{2}=='5000'
, assuming that your filename
is always in the form of
something_something_somethingofinterest_something_somethingofinterest.txt
Update:
If you like structs more than cells, then you can name your tokens like so:
parts = regexp(filename,'[^_]+_[^_]+_(?<first>[^_]+)_[^_]+_(?<second>[^\.]+)\.txt','names');
in which case parts.first=='OM2'
and parts.second=='5000'
. You can obviously name your tokens according to their actual meaning, since they are important. You just have to change first
and second
accordingly in the code above.
Update2:
If you use dir
to get your filenames, you should have a struct array with loads of unnecessary information. If you really just need the file names, I'd use a for loop like so:
files = dir('Ev_An*.txt');
for i=1:length(files)
filename=files(i).name;
parts = regexp(filename,'[^_]+_[^_]+_(?<first>[^_]+)_[^_]+_(?<second>[^\.]+)\.txt','tokens');
%here do what you want with parts.first, parts.second and the file itself
end
fopen
? Why do you need to reverse-engineer? – Andras Deakmatlab
to open'Ev_An_OM2_l5_5000.txt'
and not something else? How do you construct this filename? Do you have a database with the filenames? – Andras Deakfiles = dir('Ev_An*.txt');
to batch load hundreds of .txt files with similar filenames from the default MATLAB directory. – AnnaSchumann