Scrolling through variable and looking up file

Hi there,
I have a column vector that has a bunch of numbers. I also have a number of csv files that have numbers for names.
What I would like to do is to go through each of the numbers in the column vector and search for the file that has the name that corresponds to the number closest to the number in the column vector. For example, if the number is 121 and there are files called 110.csv, 115.csv, and 120.cvs, then I would like to select 120.csv.
Any help/guidance would be really appreciated.

답변 (2개)

Guillaume
Guillaume 2018년 10월 8일

1 개 추천

Let seek be you column vector of numbers, e.g.:
%for demo:
seek = (110:125)' %a row vector would be more practical
Let filelist be the list of files, e.g. obtained by dir
%for demo:
filelist = compose('%d.csv', [110 115 120 123])
%normaly:
%dircontent = dir(fullfile('C:\somewhere', '*.csv'));
%filelist = {dircontent.name};
Then
filenumbers = sscanf(strjoin(filelist, '\n'), '%d.csv');
[~, matchedindex] = min(abs(seek' - filenumbers));
matchedfile = filelist(matchedindex)'
Note that if seek is a row vector instead of a column vector you don't need the transpose in the last two lines.

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

태그

질문:

2018년 10월 8일

답변:

2018년 10월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by