Borrar filtros
Borrar filtros

Creating text file by combining/editing header and data

3 visualizaciones (últimos 30 días)
Nicolas
Nicolas el 6 de Jul. de 2022
Comentada: Nicolas el 19 de Jul. de 2022
Hello,
I have two different type of file with different headers sizes and different number of variables.
Fisrt one can be used by the software I have but not the second one.
First one has a 34 lines header and 8 column of data
Second one has 64 header line and 5 column of data
I want to edit/generate a readable file from the second type of file.
First : I want to recover the header from the first file
fid=fopen('file');
size_max=0;
for i=1:34
% line(i,:)=fgets(fid);
temp=size(fgets(fid),2);
size_max=max(temp, size_max);
end
frewind(fid);
line=char(' '*ones(34,size_max));
for i=1:34
temp=fgets(fid);
line(i,1:size(temp,2))=temp;
end
By doing so I get the header but I also get extra characters (arrows and carriage return)
How can a get a proper header (without extra character) ?
After doing that I want to recover data from second file. (I have a 30 line header and 5 rows of data) I use this command
datacell = textscan(fid2,'%f %f %f %f %f','headerline', 30,'Delimiter',',');
I obtain a cell which is 1x5 and which contains 1024x1 double in each row.
I am adding several column this way to make it a 1x8 cell with 1024x1 double column
comp={double(zeros(1024,1))}; %column of zeros I want to add
datacell2={[datacell comp comp comp] }; % datacell is the original data, 1x5 cell
Finally, how can I merge the header and the data into one txt file ?
I think I will use the cellwrite function (available on file exchange) but I'm not sure how to do that correctly...
Thank you for your help
PS : Sorry but I can't share the file.
  1 comentario
dpb
dpb el 6 de Jul. de 2022
Well, without at least a description of the actual file format it's imponderable...even if you don't think you can upload an actual file (and the reasons folks think that are rarely truly relevant) you can attach a sample with dummy data of the correct format. This needs to include the proper data types for each variable in the input/output files.
Which release of MATLAB are you using? Unless it's now getting to be pretty old, there should be no reason to need any FEX contributions.

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 6 de Jul. de 2022
Editada: dpb el 6 de Jul. de 2022
N1=34; % file1 header length -- don't bury magic constants inside code
N2=64; % file2 header length
hdr=readlines('File1.txt'); % read the file
hdr=hdr(1:N1); % keep the header lines only
data=readmatrix('file2.txt','NumHeaderLines',N2); % get data from second
data=[data zeros(size(data,1),3)]; % augment with three extra columns
data=join(string(data),','); % convert to string array
writematrix([hdr;data],'outputfile.txt') % and write a new output file
  3 comentarios
dpb
dpb el 19 de Jul. de 2022
Use, 'QuoteStrings',0 in the writematrix call -- see writematrix for details...
Nicolas
Nicolas el 19 de Jul. de 2022
Thank you a lot for your help !

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by