Read multiple files and concatenate

13 visualizzazioni (ultimi 30 giorni)
Momo
Momo il 11 Lug 2013
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);

Risposte (2)

dpb
dpb il 11 Lug 2013
...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 il 12 Lug 2013
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 Commento
dpb
dpb il 15 Lug 2013
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.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by