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

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

 Accepted Answer

5 Comments

Thank you Walter. I followed exactly the documentation that you suggested. See below:
for k = 1:1
textFilename = ['C:\Users\EMERSON\Desktop\1\' num2str(k) '_2013-01-21__11-35-22.txt'];
fid = fopen(textFilename, 'rt');
A=textscan(fid,'%f %f %f %f %f %f ');
fclose(fid);
end;
And it worked because I typed the entire file-name (2013-01-21_11-35-22.txt) after the index k.
But that is exactly the problem!!! The date is constant but the hour is a variable part of the file name, and therefore, I can't write a constant name after the index k. But I want to load the file by only using the index k independent on the rest variable part of the name.
I hope you know how to fix this.
Thank you again
Emerson
If you read the faq, you'd see how to use dir('C:\Users\EMERSON\Desktop\1') to get the list of filenames that are actually present in the folder, and not have to guess at how to construct one.
I read the faq and used dir('C:\Users\EMERSON\Desktop\1') to get the list of filenames that are actually present in the folder, see below:
myFolder = 'C:\Users\EMERSON\Desktop\1\';
filePattern = fullfile(myFolder, '*.txt');
txtFiles = dir(filePattern);
for k = 1:1
baseFileName = txtFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fid = fopen(fullFileName, 'rt');
A=textscan(fid,'%f %f %f %f %f %f ');
fclose(fid);
end
and it works apparently well.
However, my new problem is the order of events because if k=1:200 then
k=1 => 100_filename.txt
k=2 => 101_filename.txt
k=3 => 102_filename.txt
k=10 => 109_filename.txt
k=11 => 10_filename.txt
the result that I wish is not as above but as below:
k=1 => 1_filename.txt
k=2 => 2_filename.txt
k=3 => 3_filename.txt
k=10 => 10_filename.txt
k=11 => 11_filename.txt
Now I don't know how to sort it before it goes in the loop. I hope there is a way to correct this
Thanks
Emerson
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 ;
...

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!