Filename value extraction: code simplification

2 vues (au cours des 30 derniers jours)
Andreas B
Andreas B le 1 Déc 2017
Commenté : Andreas B le 4 Déc 2017
Being new to Matlab, it took me half a day to get my code to work at all, but now I'm wondering if it's possible to get it to work faster, in a better style and with less "detour" due to format-changing and such. It's a follow-up to a question I asked on Stack Overflow, but I find it to be more fitting to a forum like this here than S-O. Please tell me if you think it's better to stick to one platform ;)
What I want to do: I got files named abc123.xml, abc124.xml, abc133.xml etc. Now I just need the numerical value as a :,1 numerical array to be able to interactively select and process the data. This is what I got so far:
xml_filelist = dir('subfolder/*.xml'); % giving a :,1 struct with 5 fields where the first is the filename
xml_filelist = struct2table(xml_filelist); % converting it to a :,5 table where the first is the filename
xml_filelist = table2cell(xml_filelist(:,1)); % converting the first column to a cell array to be able to regexp it
xml_num = regexp(xml_filelist, '(\d+)', 'once', 'tokens'); % extracting the numerical value
xml_num = cellfun(@(x)str2double(x), xml_num); % converting it from string cell array to double values
This leaves me with an array like [123; 124; 133], which is what I need. Now is there any way to simplify the code above?

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Déc 2017
xml_filelist = dir('subfolder/*.xml');
xml_filenames = {xlm.filelist.name};
xml_num = str2double( regexp(xml_filenames, '\d+', 'match', 'once') );
  4 commentaires
Andreas B
Andreas B le 2 Déc 2017
I feel very stupid for that one, sorry. Will be the first thing I try on monday, as I it's a company license.
Andreas B
Andreas B le 4 Déc 2017
Now it works like a charm.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by