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

Jan
Jan 2016년 2월 16일
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.

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

답변 (2개)

Jan
Jan 2016년 2월 16일

0 개 추천

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

Ali Dogan
Ali Dogan 2016년 2월 16일
편집: Ali Dogan 2016년 2월 16일
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.
Walter Roberson
Walter Roberson 2016년 2월 16일
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
Walter Roberson 2016년 2월 16일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

질문:

2016년 2월 16일

답변:

2016년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by