How to overwrite a text file column keeping the headers intact while importing data from a Mat file ?

2 次查看(过去 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*

采纳的回答

Stephen23
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 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by