fprintf export data as vertical columns on csv
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
fidO = fopen('pvtsort.csv','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,','Trail',m); %print trail number header
end
fprintf(fidO,'\n')
for j = 1:length(values{1,1})
pvt = values{1,1}
fprintf(fidO,'%s,',pvt{j}); %note "the curlies" to dereference cellstr to char
fprintf(fidO,'\n')
end
fid = fclose(fid);
end
fidO= fclose(fidO);
Hi all, in the loop above, my data was exported to the csv in the format 1 Trail per row, whereas 'Trail#' prints as the header in the beginning of a row. I'd like to display my data as columns and header on top of columns instead. Long story short, I was trying to create columns with fprintf. I know if you have a set number of columns this is not that hard to do, as in the following:
a = :first
b = :second
fprintf('%f %f\n', [a;b])
or
fprintf(fid, '%f%f\n', a(:), b(:));
...something like that. Problem is, as seen in my code above, I will be putting this code in a folder with no set amount of text files. This code automatically detects the number of txt files and reads into them, so I can't assign variable name to my data strings as in the example above. Any idea how I can format my data into column display? Will functions like cell2mat help? As in converting my cell arrays to a matrices, then read into the vectors etc.
Thanks for looking,
0 comentarios
Respuestas (1)
Nstaub
el 2 de Sept. de 2016
Hi,
did you try to use fprintf with a string and a matrix, something like :
str = [];
matrix = [];
for i = i:length(d)
str = [str,'%f '];
matrix = [matrix , 'here add the data you want'];
...
end
str = [str,'\n'];
fprintf(str,matrix);
This is a quick fix it's a bit nasty because the size are not assigned. You can do something nicer ;)
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!