필터 지우기
필터 지우기

Creating text file by combining/editing header and data

조회 수: 3 (최근 30일)
Nicolas
Nicolas 2022년 7월 6일
댓글: Nicolas 2022년 7월 19일
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
dpb
dpb 2022년 7월 6일
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.

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

채택된 답변

dpb
dpb 2022년 7월 6일
편집: dpb 2022년 7월 6일
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
dpb
dpb 2022년 7월 19일
Use, 'QuoteStrings',0 in the writematrix call -- see writematrix for details...
Nicolas
Nicolas 2022년 7월 19일
Thank you a lot for your help !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by