フィルターのクリア

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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by