fprintf one data per cell from text to csv

조회 수: 2 (최근 30일)
Tammy Chen
Tammy Chen 2016년 8월 26일
댓글: Star Strider 2016년 8월 30일
Hi all, I've written the following code to help me copy the third column from multiple text files to one csv spreadsheet in the following format: header is 'Trails', followed by third column of each text file displayed horizontally per row (thus no \n).
fidO = fopen('pvtsort.csv','wt');
d = dir('*txt');
for i = 1:length(d)
fid = fopen(d(i).name, 'r'); % open all input txt files, no array
values = textscan(fid,'%*s%*s%s','HeaderLines',4, 'Delimiter','\t');% read only the 3rd column
fprintf(fidO,'%s\t','Trails');
for j = 1:length(values{1,1})
pvt = values{1,1}
fprintf(fidO,'%s\t',pvt{j}); %note "the curlies" to dereference cellstr to char
end
fprintf(fidO,'\r\n')
fid = fclose(fid);
end
fidO = fclose(fidO)
Well the output looks fine when I open the csv in matlab, with one data point occupying one cell, one column of data per row with header "Trails" in front. However, when I open my output spreadsheet 'pvtsort.csv' directly outside matlab, it looks funny. All the data points from one column are crammed into a cell instead of separate cells like how the csv was displayed in matlab (as if the '\t' didn't work). I played around with several delimiters like \v, \r,combinations of these with '\t', but could not for the life of me figure out why data wasn't displayed properly when opening csv outside matlab. Pointers appreciated.

채택된 답변

Star Strider
Star Strider 2016년 8월 27일
Just guessing here since I don’t have your files and so can’t run your code, but since .csv indicates comma-separated variables, perhaps:
fprintf(fidO,'%s, ','Trails');
and
fprintf(fidO,'%s, ',pvt{j}); %note "the curlies" to dereference cellstr to char
could work.
  댓글 수: 2
Tammy Chen
Tammy Chen 2016년 8월 29일
Thank you so much for the prompt answer. You suggestion regarding file format is a good point. I didn't try switching the delimiter to comma (because I want my output file to use the same delimiter as my input text files, which use '\t'), but simply switched the output file format to xls and viola, it displayed in the format I wanted. So this is what I have now:
fidO = fopen('pvtsort.xls','w+');
d = dir('*txt');
for i = 1:length(d)
fid = fopen(d(i).name, 'r'); % open all input txt files, no array
values = textscan(fid,'%*s%*s%s','HeaderLines',4, 'Delimiter','\t');% read only the 3rd column
for n = 1
m = num2str(n * i)
fprintf(fidO,'%s%s\t','Trail',m);
end
for j = 1:length(values{1,1})
pvt = values{1,1}
fprintf(fidO,'%s\t',pvt{j}); %note "the curlies" to dereference cellstr to char
end
fprintf(fidO,'\n')
fid = fclose(fid);
end
fidO = fclose(fidO)
Not the final output yet because I still want Matlab to do some percentile calculations for me, but hope this helps someone out there.
Star Strider
Star Strider 2016년 8월 30일
My pleasure.
I apologise for the delay in replying, but was locked out by some sort of ‘Scheduled Maintenance’ for the last few hours.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by