Filename value extraction: code simplification

조회 수: 2 (최근 30일)
Andreas B
Andreas B 2017년 12월 1일
댓글: Andreas B 2017년 12월 4일
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?

채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 1일
xml_filelist = dir('subfolder/*.xml');
xml_filenames = {xlm.filelist.name};
xml_num = str2double( regexp(xml_filenames, '\d+', 'match', 'once') );
  댓글 수: 4
Andreas B
Andreas B 2017년 12월 2일
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 2017년 12월 4일
Now it works like a charm.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by