필터 지우기
필터 지우기

Read multiple files and concatenate

조회 수: 5 (최근 30일)
Momo
Momo 2013년 7월 11일
Hello, I would like to concatenate binary files to a matrix and I am using the code below. The problem is that I have files tarting from 3201.rad to 3396.rad, and by doing the the sprintf(3%ddd.rad,i), it can read only to 3299.rad If somebody knows how to solve this problem. Thank you in advance.
% code
numfiles = 196;
concatC = cell(1, numfiles);
for i = 1:numfiles
filename = sprintf('3%ddd.rad', i);
Str = strrep(fileread(filename), ',', '.');
CStr = regexp(Str, '\n', 'split');
CStr(strncmp(CStr, 'F', 1)) = [];
% Perhaps:
if isempty(CStr{end})
CStr(end) = [];
end
concatC{i} = CStr(:);
end
concat = cat(1, concatC{:});
newFile = fullfile(tempdir, 'Raw.dat');
FID = fopen(newFile, 'w');
if FID == -1, error('Cannot open file for writing'); end
fprintf(FID, '%s', concat{:});
fclose(FID);

답변 (2개)

dpb
dpb 2013년 7월 11일
...3201.rad to 3396.rad, and by doing the the sprintf(3%ddd.rad,i),..
'3%ddd.rad' as a format string will output '3201dd.rad' for an input value of 201 not '3201.rad' as I would presume you're wanting. That would be '3%3d.rad' for the format string instead.
You can use the same idea but just change the range to be 3201:3396, say, and the format string to '%4d.rad'
But, I'd suggest use
d=dir('*.rad');
for i=1:length(d)
fn=d.name(i) % i'th name
If need be you can qualify the wild card '*' and/or select from the entire list to get only the ones desired.

Momo
Momo 2013년 7월 12일
Thank you dpb for your answer and help. I still have some problems. Your both suggestions do not work. Maybe I did not explain well, what I want to do. Well, I have 196 files with names starting from 3201.rad to 3396.rad. Each file has a header (5 lines of text), 1057 raws, 3 colonnes. I want to concatenate all the 196 files and convert them into matrix. For the conversion into a matrix I know how to do it, but for concatenation I have some problems my code do not read the files. Thank you in advance for your help. M.
  댓글 수: 1
dpb
dpb 2013년 7월 15일
What, precisely, are you meaning w/ "concatenate", here?
If the files each contain a header and some data, then a matrix can't be made of them unless it's a cell array.
Or do you want to remove the headers and make a single large numeric array?
And, it doesn't help much (like any) to only say "do not work". What, specifically happened including error messages and enough code to make sense thereof.
I presumed from your previous that you said you knew how to make the matrix that if you got past the name part that certainly traversing the dir() structure should do, you would then be set...
Not knowing precisely what is really wanted, didn't have enough information to do the rest.

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by