concatenate and replace comma by dot

Hello, I have a series of 300 files with names respectively from data-1.dat to data300.dat. Each file consist of 6 columns and 25 rows (files contain scalars with comma instead of dot, ex: 3,21223232 and a header line) I want to concatenate all files into one matrix and I'm using the code below:
% code
numfiles = 300;
concat= cell(1, numfiles);
for i=1:300;
i
filename = sprintf(data-%d.dat, i);
concat{i} = importdata(filename, '\t');
end
the_struct = [concat{:}];
M = [the_struct.data];
For data that doesn't have a comma the code work very well, otherwise it doesn't. Anyhelpful answer is very welcome. Thank you in advance.

 Respuesta aceptada

Jan
Jan el 17 de Dic. de 2012
Editada: Jan el 18 de Dic. de 2012
numfiles = 300;
concat = cell(1, numfiles);
for ii = 1:numfiles
filename = sprintf('data-%d.dat', ii);
concat{ii} = strrep(fileread(filename), ',', '.');
end
newFile = fullfile(tempdir, 'JoinedFile.dat');
FID = fopen(newFile, 'w');
if FID == -1, error('Cannot open file for writing'); end
fprintf(FID, '%s', concat{:});
fclose(FID);
Now the new file contains all data with dots instead of commas and can be imported at once.
[EDITED] Remove headerlines, which start with 'F':
concatC = cell(1, numfiles);
for ii = 1:numfiles
filename = sprintf('data-%d.dat', ii);
Str = strrep(fileread(filename), ',', '.');
CStr = regexp(Str, '\n', 'split');
CStr(strncmp(CStr, 'F', 1)) = [];
% Perhaps:
if isempty(CStr{end})
CStr(end) = [];
end
concatC{ii} = CStr(:);
end
concat = cat(1, concatC{:});

8 comentarios

Momo
Momo el 18 de Dic. de 2012
Thank you very much it help a lot. However, I'm still stack with the new file how to remove the header line (ex: each 25 row I have a text data "headerline"). Thank you in advance.
Jan
Jan el 18 de Dic. de 2012
How can these headerlines be recognized? An explicit example would be helpful.
Momo
Momo el 18 de Dic. de 2012
Editada: Momo el 18 de Dic. de 2012
Headerline looks like: Frequency R X sigma sigmasec M Msec
Jan
Jan el 18 de Dic. de 2012
Editada: Jan el 18 de Dic. de 2012
Sorry, Momo, does this mean that all header lines can be detected by a leading 'F' character? If so, they can be removed easily before writing the joined data file.
Momo
Momo el 18 de Dic. de 2012
Editada: Momo el 18 de Dic. de 2012
Yes Jan they are detected by an 'F' character.
Momo
Momo el 19 de Dic. de 2012
Is there an answer to my question??? Thank you in advance.
Jan
Jan el 20 de Dic. de 2012
@Momo: Have you seen the [EDITED] section in my answer? If not, do you have any reason to be inpatient?
Momo
Momo el 20 de Dic. de 2012
@Jan: No I just have seen it. Sorry for been inpatient (It's because I had a deadline). Thank you very much for your precious help.
M.

Iniciar sesión para comentar.

Más respuestas (1)

Momo
Momo el 18 de Dic. de 2012
When I try to import data
all_data= importdata('JoinedFile.dat');
It import data as a text, colheader and data with only the first set of 25 rows and 6 columns(i.e only first file), and leave 299.
Best regards,

Preguntada:

el 17 de Dic. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by