How to overwrite a text file column keeping the headers intact while importing data from a Mat file ?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi all !!!
I have to create my own input data file by replacing the old values given in the .dat* file. I am struggling with the logic here.
Can anyone help me in this. I a attaching the .dat* file here. I need to change all column values with my own values one after one which I have in Mat files. While doing all this I am supposed to keep the column heading as it is.
Here the original format of the file was .dat* but as it could not be attached I changed it to .txt*
댓글 수: 0
채택된 답변
Stephen23
2014년 12월 23일
You can do this easily using fread to read the data into ones string, textscan to conver the data to numeric values, then save it all again using fprintf:
% Read all of the data into one string:
fid = fopen('test.txt','rt');
S = fread(fid,'*char')';
fclose(fid);
% Locate the start of the numeric data:
idx = regexpi(S,'^<forcing>.+?$','lineanchors','end');
% Convert to numeric:
C = textscan(S(2+idx:end),'%u%u%u%u%u%f%f%f%f%f%f%f%f');
% Datestamps:
D = [C{:,1:5}];
% Other data:
E = [C{:,6:end}];
%
% Do your magic data processing here...
%
% Save the data:
fid = fopen('test_new.txt','wt');
fprintf(fid,'%s',S(1:idx));
for k = 1:size(D,1)
fprintf(fid,'\n%04d %02d %02d %02d %02d',D(k,:));
fprintf(fid,' %16.10f',E(k,:));
end
fclose(fid);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!