Selecting all folders using textscan.

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

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.

Sign in to comment.

Answers (2)

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

Thanks, But i dont understand clearly. Thats my cod;
for i=1:3
k1 = fopen('*/x/koo.txt');
c1 = textscan(k1,'%f %f %f');
fclose(k1);
x=c1{1};
y=c1{2};
z=c1{3};
k2 = fopen('*/y/std.txt');
c2 = textscan(k2,'%f %f %f');
fclose(k2);
stx=c2{1};
sty=c2{2};
stz=c2{3};
xyz(iFile,1)=x;
xyz(iFile,2)=y;
xyz(iFile,3)=z;
stt(iFile,1)=stx;
stt(iFile,2)=sty;
stt(iFile,3)=stz;
end
xyz
stt
and thats the error message.
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.

Sign in to comment.

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.)

Tags

Asked:

on 16 Feb 2016

Answered:

on 16 Feb 2016

Community Treasure Hunt

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

Start Hunting!