Put cell array data into txt file

Hi everyone.
I need help.
I have a complete function snippet. I only have 1 thing left to do is to export the data of the cell array: H. I need to put that data into a txt file.
I will put the function below.
Thank you.
Function:
function H=baitap
% read the entire file
fid = fopen('log.txt');
data = fread(fid,'*char').';
fclose(fid);
% cell array C, with each element containing one line of text
C = strsplit(data,newline());
% keep only lines saying 'Printing Done'
C = C(contains(C,'Printing Done'));
% grab the dates/times from those lines
dates = regexp(C,'(\d+-\d+-\d+ \d+:\d+:\d+)','tokens','once');
dates = vertcat(dates{:});
A=dates;
%Date and total
H=cell(32,2);
%Date
H{1,1}='Ngay';
H{2,1}='1-3-2020';
H{3,1}='2-3-2020';
H{4,1}='3-3-2020';
H{5,1}='4-3-2020';
H{6,1}='5-3-2020';
H{7,1}='6-3-2020';
H{8,1}='7-3-2020';
H{9,1}='8-3-2020';
H{10,1}='9-3-2020';
H{11,1}='10-3-2020';
H{12,1}='11-3-2020';
H{13,1}='12-3-2020';
H{14,1}='13-3-2020';
H{15,1}='14-3-2020';
H{16,1}='15-3-2020';
H{17,1}='16-3-2020';
H{18,1}='17-3-2020';
H{19,1}='18-3-2020';
H{20,1}='19-3-2020';
H{21,1}='20-3-2020';
H{22,1}='21-3-2020';
H{23,1}='22-3-2020';
H{24,1}='23-3-2020';
H{25,1}='24-3-2020';
H{26,1}='25-3-2020';
H{27,1}='26-3-2020';
H{28,1}='27-3-2020';
H{29,1}='28-3-2020';
H{30,1}='29-3-2020';
H{31,1}='30-3-2020';
H{32,1}='31-3-2020';
%Size matrix
R1=size([250:655]);
R2=size([656:957]);
R3=size([958:1203]);
R4=size([1204:1468]);
R5=size([1469:1761]);
R6=size([1762:1911]);
R7=size([1912:2015]);
R8=size([2016:2347]);
R9=size([2348:2623]);
R10=size([2624:2883]);
R11=size([2884:3151]);
R12=size([3152:3384]);
R13=size([3385:3520]);
R14=size([3521:3646]);
R15=size([3647:3965]);
R16=size([3966:4287]);
R17=size([4288:4542]);
R18=size([4543:4823]);
R19=size([4824:5099]);
R20=size([5100:5285]);
R21=size([5286:5401]);
R22=size([5402:5701]);
R23=size([5702:5980]);
R24=size([5981:6252]);
R25=size([6253:6479]);
R26=size([6480:6707]);
R27=size([6708:6898]);
R28=size([6899:7017]);
R29=size([7018:7353]);
R30=size([7354:7625]);
R31=size([1:249]);
%Total
H{1,2}='So_Luong';
H{2,2}=R1(1,2);
H{3,2}=R2(1,2);
H{4,2}=R3(1,2);
H{5,2}=R4(1,2);
H{6,2}=R5(1,2);
H{7,2}=R6(1,2);
H{8,2}=R7(1,2);
H{9,2}=R8(1,2);
H{10,2}=R9(1,2);
H{11,2}=R10(1,2);
H{12,2}=R11(1,2);
H{13,2}=R12(1,2);
H{14,2}=R13(1,2);
H{15,2}=R14(1,2);
H{16,2}=R15(1,2);
H{17,2}=R16(1,2);
H{18,2}=R17(1,2);
H{19,2}=R18(1,2);
H{20,2}=R19(1,2);
H{21,2}=R20(1,2);
H{22,2}=R21(1,2);
H{23,2}=R22(1,2);
H{24,2}=R23(1,2);
H{25,2}=R24(1,2);
H{26,2}=R25(1,2);
H{27,2}=R26(1,2);
H{28,2}=R27(1,2);
H{29,2}=R28(1,2);
H{30,2}=R29(1,2);
H{31,2}=R30(1,2);
H{32,2}=R31(1,2);
% write data into txt
end
Thank you.

回答(1 个)

hello
try this
% writing dates to output file
fid = fopen('output.txt','w');
fprintf(fid,'%s\n',H{:});
fclose(fid);
% checking the output file
type('output.txt');

5 个评论

Thank you <3.
I tried but it has a little problem. I will leave the image output.txt. Looks like it's messed up.
This is the style i need. result_example.txt:
ok
can you simply share the H array as mat file pls ?
I prefer to check my code before I post it
tx
Okay. It's quite long
%Date and total
H=cell(32,2);
%Date
H{1,1}='Ngay';
H{2,1}='1-3-2020';
H{3,1}='2-3-2020';
H{4,1}='3-3-2020';
H{5,1}='4-3-2020';
H{6,1}='5-3-2020';
H{7,1}='6-3-2020';
H{8,1}='7-3-2020';
H{9,1}='8-3-2020';
H{10,1}='9-3-2020';
H{11,1}='10-3-2020';
H{12,1}='11-3-2020';
H{13,1}='12-3-2020';
H{14,1}='13-3-2020';
H{15,1}='14-3-2020';
H{16,1}='15-3-2020';
H{17,1}='16-3-2020';
H{18,1}='17-3-2020';
H{19,1}='18-3-2020';
H{20,1}='19-3-2020';
H{21,1}='20-3-2020';
H{22,1}='21-3-2020';
H{23,1}='22-3-2020';
H{24,1}='23-3-2020';
H{25,1}='24-3-2020';
H{26,1}='25-3-2020';
H{27,1}='26-3-2020';
H{28,1}='27-3-2020';
H{29,1}='28-3-2020';
H{30,1}='29-3-2020';
H{31,1}='30-3-2020';
H{32,1}='31-3-2020';
%Size matrix
R1=size([250:655]);
R2=size([656:957]);
R3=size([958:1203]);
R4=size([1204:1468]);
R5=size([1469:1761]);
R6=size([1762:1911]);
R7=size([1912:2015]);
R8=size([2016:2347]);
R9=size([2348:2623]);
R10=size([2624:2883]);
R11=size([2884:3151]);
R12=size([3152:3384]);
R13=size([3385:3520]);
R14=size([3521:3646]);
R15=size([3647:3965]);
R16=size([3966:4287]);
R17=size([4288:4542]);
R18=size([4543:4823]);
R19=size([4824:5099]);
R20=size([5100:5285]);
R21=size([5286:5401]);
R22=size([5402:5701]);
R23=size([5702:5980]);
R24=size([5981:6252]);
R25=size([6253:6479]);
R26=size([6480:6707]);
R27=size([6708:6898]);
R28=size([6899:7017]);
R29=size([7018:7353]);
R30=size([7354:7625]);
R31=size([1:249]);
%Total
H{1,2}='So_Luong';
H{2,2}=R1(1,2);
H{3,2}=R2(1,2);
H{4,2}=R3(1,2);
H{5,2}=R4(1,2);
H{6,2}=R5(1,2);
H{7,2}=R6(1,2);
H{8,2}=R7(1,2);
H{9,2}=R8(1,2);
H{10,2}=R9(1,2);
H{11,2}=R10(1,2);
H{12,2}=R11(1,2);
H{13,2}=R12(1,2);
H{14,2}=R13(1,2);
H{15,2}=R14(1,2);
H{16,2}=R15(1,2);
H{17,2}=R16(1,2);
H{18,2}=R17(1,2);
H{19,2}=R18(1,2);
H{20,2}=R19(1,2);
H{21,2}=R20(1,2);
H{22,2}=R21(1,2);
H{23,2}=R22(1,2);
H{24,2}=R23(1,2);
H{25,2}=R24(1,2);
H{26,2}=R25(1,2);
H{27,2}=R26(1,2);
H{28,2}=R27(1,2);
H{29,2}=R28(1,2);
H{30,2}=R29(1,2);
H{31,2}=R30(1,2);
H{32,2}=R31(1,2);
there is an obvious solution in one line :
writecell(H,'output.txt','Delimiter','tab')
Thank you. I will try after update new version :'D. I am currently using r2018a version.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Language Fundamentals 的更多信息

产品

版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by