selecting multiple files from folder directly

30 ビュー (過去 30 日間)
shanmukh
shanmukh 2013 年 5 月 14 日
i have a folder DATA in which i have 3 file with names
a1_14.05.2013_1_a.txt
a1_15.05.2013_2_a.txt
a1_14.05.2013_3_a.txt
i need to select folder DATA to grab 3 different files starting with a1,1 and a1,2 and a1,3 irrespective of the dates.
please let me know how to do this

回答 (4 件)

David Sanchez
David Sanchez 2013 年 5 月 14 日
Hello, this might be of help:
% this creates a structure with information on every
% file whose name starts with "a1_" and has ".txt" extension.
files_wanted = dir('a1_*.txt');
files_wanted(1).name % presents the name of first file agreeing your search criteria
  1 件のコメント
shanmukh
shanmukh 2013 年 5 月 14 日
編集済み: shanmukh 2013 年 5 月 14 日
i need file starting with a1 and having 1 in the middle

サインインしてコメントする。


David Sanchez
David Sanchez 2013 年 5 月 14 日
adapt the line to your need:
files_wanted = dir('a1*1*.txt');
  2 件のコメント
shanmukh
shanmukh 2013 年 5 月 14 日
d = uigetdir(pwd, 'Select a folder');
files_wanted = dir('a1*1*.txt');
fid1 = fopen((files_wanted), 'r');
c2 = textscan(files_wanted,'%s%f%f%f%f%f%f','delimiter', ';','HeaderLines',1);
i am trying to get data from the file but i have an error message '
Error using fopen
First input must be a file name of type char, or a file identifier of type double.'
Jan
Jan 2013 年 5 月 14 日
dir() replies a struct array, as the documentation explains exhaustively. You want the name of the file as string:
[fName, fPath] = uigetdir(pwd, 'Select a folder');
files_wanted = dir(fullfile(fPath, 'a1*1*.txt'));
FileName = fullfile(fPath, files_wanted(1).name);
fid1 = fopen(FileName, 'r');

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2013 年 5 月 14 日
variant
q = dir('*.txt');
n = regexp({q.name},'^a1.*[1-3]_a.*','match');
out = n(~cellfun('isempty',n));

David Sanchez
David Sanchez 2013 年 5 月 14 日
Firstly, if you use uigetdir, there is no need for files_wanted = dir('a1*1*.txt'). Use one or the other method to open the files.
You can also use
uigetfile('a1*.txt')
to look for your files and choose them one at a time. Use it recursively if you want to.
In your code, you are passing and structure to fopen function, which can not be performed.

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by