Extracting values from file names to array
조회 수: 4 (최근 30일)
이전 댓글 표시
Let's say I have a directory "mydirectory" and in it, I have some files
file_234.txt
file_356.txt
file_567.txt
file_987.txt
and I want to form an array in Matlab out of the respective numbers.
234
356
567
987
How can I go into the directory, extract the information from the files, and put each value into the array?
Thanks!
댓글 수: 0
채택된 답변
Stephen23
2019년 10월 22일
편집: Stephen23
2019년 10월 22일
>> D = 'path to the folder where the files are saved';
>> S = dir(fullfile(D,'file_*.txt'));
Method one: regexp:
>> V = str2double(regexp({S.name},'\d+','match','once'))
V =
234 356 567 987
Method two: sscanf:
>> V = sscanf([S.name],'file_%d.txt') % change to suit your filenames
V =
234
356
567
987
댓글 수: 0
추가 답변 (1개)
Sean de Wolski
2019년 10월 22일
fn = ["file_234.txt"
"file_356.txt"
"file_567.txt"
"file_987.txt"]
double(extractBetween(fn, "_", "."))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!