Facing problems in writing data to a .dat file?

8 views (last 30 days)
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

Accepted Answer

Preyanka Dey
Preyanka Dey on 2 Oct 2020
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

More Answers (0)

Categories

Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!