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.

Respuestas (2)

Guillaume
Guillaume el 8 de Oct. de 2018
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.

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Oct. de 2018

Respondida:

el 8 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by