필터 지우기
필터 지우기

How to import a file using only part of the name, such as an index at the begin of filename?

조회 수: 9 (최근 30일)
INTRO Hi, I have multiple text files named as below:
C:\Users\EMERSON\Desktop\1\1_2013-01-21__15-35-22.txt
C:\Users\EMERSON\Desktop\1\2_2013-01-21__15-35-23.txt
C:\Users\EMERSON\Desktop\1\3_2013-01-21__15-35-24.txt
C:\Users\EMERSON\Desktop\1\4_2013-01-21__15-35-25.txt
C:\Users\EMERSON\Desktop\1\5_2013-01-21__15-35-26.txt
C:\Users\EMERSON\Desktop\1\6_2013-01-21__15-35-27.txt
C:\Users\EMERSON\Desktop\1\7_2013-01-21__15-35-28.txt
The file name is made of three components: i) index k=1:arbitrary, ii) date, iii)hour.
And I want to import/load these files using only the first index (1,2,3,4,5,6,7). So I wrote:
for k = 1:1;
fid =fopen(['C:\Users\EMERSON\Desktop\1\',num2str(k),'~.txt']);
A=textscan(fid,'%f %f %f %f %f %f ');
fclose(fid);
end
For instance I use k=1:1 only to test if one file is imported correctly, later k=1:arbitrary.
Problem: I obtain the following error:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
I wonder if someone could tell me how to correct the first command line (fid=fopen....).
Thank you in advance for your attention
Emerson

채택된 답변

Walter Roberson
Walter Roberson 2013년 1월 21일
  댓글 수: 5
Cedric
Cedric 2013년 1월 22일
편집: Cedric 2013년 1월 22일
There is certainly a function that will allow sorting according to what you need in one shot. Here would be one way to do it "by hand" until someone tells you how to do it properly:
txtFiles = ... (the way you define it)
[~,id] = sort(cellfun(@(n)sscanf(n,'%d_'), {txtFiles(:).name})) ;
for k = 1 : numel(txtFiles)
baseFileName = txtFiles(id(k)).name ;
...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by