Write cell array, table into text file?
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Peter Fang
am 23 Apr. 2022
Bearbeitet: Stephen23
am 23 Apr. 2022
hello again guys.
I have 1 question: I used cell2table to be able to use writetable, but the output data stick together, it doesn't match the model given by the teacher.
My result name: exe.txt
The result the teacher needs: result_example.txt
I'm asking myself, if I concatenate the words in cell array H will it solve the problem?
I will leave the resulting file and image below, including the m file of the function.
Hope you can help. Thank you.
P/s: I use R2018a


1 Kommentar
Akzeptierte Antwort
Stephen23
am 23 Apr. 2022
Bearbeitet: Stephen23
am 23 Apr. 2022
Fake data just for testing the code:
dtm = datetime(2021,3,[1;2;31],'Format','dd-MM-yyyy')
num = [15;10;2]
Connvert data (from table or whatever you have) into one cell array:
tmp = num2cell(reshape(dtm,1,[]));
tmp(2,:) = num2cell(num);
Header:
hdr = {'Ngay','So luong'};
hdr = regexprep(hdr,' ','\a');
Now print fixed-width text using tab separator:
str = [...
sprintf('%-16s%-s\n',hdr{:}),...
sprintf('%-16s%-u\n',tmp{:})];
str = regexprep(str,' {1,4}','\t');
str = regexprep(str,'\a',' ');
[fid,msg]= fopen('result.txt','wt');
fprintf(fid,'%s',str);
fclose(fid);
Check file content:
type result.txt
It is a shame that FPRINTF does not have an option for padding with tabs.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!