Read multiple files and concatenate
조회 수: 14 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
답변 (2개)
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.
댓글 수: 0
Momo
2013년 7월 12일
댓글 수: 1
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 Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!