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

3 visualizaciones (últimos 30 días)
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*

Respuesta aceptada

Stephen23
Stephen23 el 23 de Dic. de 2014
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);

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by