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
2018 年 10 月 8 日
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!