Facing problems in writing data to a .dat file?

2 ビュー (過去 30 日間)
Preyanka Dey
Preyanka Dey 2020 年 10 月 2 日
回答済み: Preyanka Dey 2020 年 10 月 2 日
Hi everyone!
After extracting data from the attached datapp.txt file when I tried to write it again in a output.dat file, I am not getting the results the way I want. I guess its because the variables are read as a character array. However, I don't know how to deal with it. The written output.dat file should be in the following format.....
TS_sec gage5 gage10 gage13 gage19
0.00000000e+00 3.74000000e+00 4.62000000e+00 -6.32000000e+00 3.70000000e+00
3.62884792e+03 4.06626222e+00 4.94714691e+00 -6.02841756e+00 4.02629196e+00
7.37845996e+03 3.70195900e+00 4.58459573e+00 -6.37805257e+00 3.66177872e+00
I have tried the following code.........Could you guys please help me in this regard. Thanks....## My Problem is in writing output files in the above mentioned format.
function main
A = regexp(fileread('datap.dat'),'\n','split');
Whichline = find(~cellfun('isempty',...
strfind(A,'TS 0 ')))
gage5 = [];
gage10 = [];
gage13 = [];
gage19 = [];
TS_sec = [];
idx=[Whichline];
n = length(idx);
for i = 1:n
TimeStp = regexp(A{idx(i)},' ','split');
TS_sec = [TS_sec; TimeStp{4}];
gage5 = [gage5; A{idx(i)+5}];
gage10 = [gage10; A{idx(i)+10}];
gage13 = [gage13; A{idx(i)+13}];
gage19 = [gage19; A{idx(i)+19}];
end
% 1st Option
gtable = table(TS_sec,gage5,gage10,gage13,gage19);
writetable(gtable,'gtable.dat','WriteVariableNames',true);
%2nd option
%write_files('gtables.dat', TS_sec, gage5, gage13, gage19);
end
function write_files(fname, TS_sec, gage5, gage13, gage19)
fid = fopen(fname, 'w');
fprintf('%s %s %s %s\n',[TS_sec gage5 gage13 gage19]');
fprintf(fid,'%s %s %s %s\n',[TS_sec gage5 gage13 gage19]');
fclose(fid);
end

採用された回答

Preyanka Dey
Preyanka Dey 2020 年 10 月 2 日
Yeyy! I solved it...following is the code...if somebody need it...
function main
A = regexp(fileread('datap.dat'),'\n','split');
Whichline = find(~cellfun('isempty',...
strfind(A,'TS 0 ')))
gage5 = [];
gage10 = [];
gage13 = [];
gage19 = [];
TS_sec = [];
idx=[Whichline];
n = length(idx);
for i = 1:n
TimeStp = regexp(A{idx(i)},' ','split');
TS_sec = [TS_sec; TimeStp{4}];
gage5 = [gage5; A{idx(i)+5}];
gage10 = [gage10; A{idx(i)+10}];
gage13 = [gage13; A{idx(i)+13}];
gage19 = [gage19; A{idx(i)+19}];
end
gage5_m= str2num(gage5);
gage10_m= str2num(gage10);
gage13_m= str2num(gage13);
gage19_m= str2num(gage19);
T_sec= str2num(TS_sec);
%1st Option
gtable = table(T_sec, gage5_m, gage10_m, gage13_m, gage19_m);
writetable(gtable,'gtable.dat','WriteVariableNames',true);
write_files('Sroy.dat', T_sec, gage5_m, gage10_m, gage13_m, gage19_m);
end
function write_files(fname, T_sec, gage5_m, gage10_m, gage13_m, gage19_m)
fid = fopen(fname, 'w');
fprintf('%f %f %f %f %f\n',[T_sec gage5_m gage10_m gage13_m gage19_m]');
fprintf(fid,'%f %f %f %f %f\n',[T_sec gage5_m gage10_m gage13_m gage19_m]');
fclose(fid);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeClassical Control Design についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by