Selecting all folders using textscan.
Show older comments
Hi, I have some text files and i want to use textscan.
k1 = fopen('*/x/koo.txt');
c1 = textscan(k1,'%f %f %f');
fclose(k1);
I tried this code but it didnt work.
Thats my files with directories.
a/x/koo.txt
a/y/std.txt
b/x/koo.txt
b/y/std.txt
c/x/koo.txt
c/y/std.txt
How can i do it?
1 Comment
Jan
on 16 Feb 2016
Whenever you write "didn't work" in the forum, post the error message or explain the difference between you expectations and the results. It is easier to solve a problem than to guess it.
Answers (2)
Jan
on 16 Feb 2016
Why do you assume that fopen('*/x/koo.txt') could find a file? Which file should be taken in your opinion? Most of all "c/y/std.txt" cannot be reached in any way.
FileList = {'a/x/koo.txt', 'a/y/std.txt', 'b/x/koo.txt', ...
'b/y/std.txt', 'c/x/koo.txt', 'c/y/std.txt'};
C = cell(1, numel(FileList));
for iFile = 1:numel(FileList)
fid = fopen(FileList{iFile}, 'r');
if fid == -1, error('Cannot open file: %s', FileList{iFile}); end
C{iFile} = textscan(k1, '%f %f %f');
fclose(fid);
end
2 Comments
Walter Roberson
on 16 Feb 2016
It is not possible to fopen() a file that uses "*" as part of the name. Well, it is, but the "*" has to really be present in the name. In other words, fopen() does not do any handling of wildcards: you need to tell it exactly which file you want to process, and it can only process one file at a time.
Walter Roberson
on 16 Feb 2016
I suggest you use a File Exchange Contribution that supports searching for multiple files; you would search for koo.txt and std.txt files. The output would be in the same kind of structure that is used by dir(), so you would access the .name field of each item to determine the name
dinfo = dir2(pwd, 'koo.txt', 'std.txt', '-r');
for K = 1 : length(dinfo)
thisfile = dinfo(K).name;
fid = fopen(thisfile, 'rt');
c = textscan(fid, '%f %f %f');
fclose(fid);
c1{K,1} = c{1};
c1{K,2} = c{2};
c1{K,3} = c{3};
c1{K,4} = thisfile;
end
The end result would be a something-by-4 cell array in which for any one row of the cell array, the first 3 columns correspond to the columns of input, and the 4th column has the file name (in case you care later.)
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!