fprintf specific column from multiple text files to one csv sheet

3 visualizaciones (últimos 30 días)
Hi all, I previously wrote a short simple script to copy the third column of a text file to a new csv sheet (as shown below).
fid = fopen('pvt-results_1496-023B.txt','r');
parameters = textscan(fid, '%s%s%s\n','HeaderLines',4, 'Delimiter','\t');
fclose(fid);
gid = fopen('pvtsort.csv','wt');
fprintf(gid,'%s\n','Trail');
for k = parameters{1,3}
i = 1:length(k)
fprintf(gid,'%s\n',k{i})
end
fclose(gid)
So this worked fine. Now I'm trying to loop this script in order to copy the third column from multiple text files and put them all on a single csv sheet with headers (Trail [incremental numbers for each additional column]). My .m file is in the folder with all text files (same # of headerlines & columns, different # of rows), but I got stuck after loading multiple text files with
fid = dir('*txt')
for i = 1:length(fid)
files(i) = fopen(fid(i).name);
fid(i).values = textscan(files(i), '%s%s%s\n','HeaderLines',4, 'Delimiter','\t');
fclose(files(i));
end
gid = fopen('pvtsort.csv','wt');
So I was trying to fprinf the third column from multiple text files to 'pvtsort.csv' but keep getting the error "Error using fprintf. Function is not defined for 'cell' inputs." So I tried loading the files differently using numel instead of length:
for k = 1:numel(fid)
pvt = fopen(fid{k},'r');
content(k) = textscan(pvt,'%s%s%s\n','HeaderLines',4,'Delimiter','\t');
data{k} = content{3};
fclose(pvt);
end
And got "Cell contents reference from a non-cell array object. " I guess I'm loading the txt files incorrectly here with the dir() function, which is why I can't even begin copy anything from the files, let along the specific column. When I tried to load the 16 txt files in my folder with fid = dir('*txt'), it throws the following info fid =16x1 struct array with fields:
  • name
  • date
  • bytes
  • isdir
  • datenum
I think the struct array is the issue here? Perhaps I should not use fprintf function to copy a string of data from multiple txt files onto a csv?
Pointers appreciated.

Respuesta aceptada

dpb
dpb el 26 de Ag. de 2016
"Error using fprintf. Function is not defined for 'cell' inputs."
Indeed it isn't; must cast to character array...
fidO=fopen('pvtsort.csv','wt'); % open the output file
d = dir('*txt'); % 'fid' sounds too much like a file handle, not a directory listing...
for i = 1:length(d)
fid=fopen(d(i).name); % don't need the input file but once; no array
values=textscan(fid,'%*s%*s%s','HeaderLines',4, 'Delimiter','\t'); % read only the 3rd column
fid=fclose(fid);
% now write the data to the new file; must loop for character data
for j=1:length(values)
fprintf(fidO,'%s\n',values{i}); % note "the curlies" to dereference cellstr to char
end
end
fidO=fclose(fidO);
If you want a header line first, write it after opening the output file before starting the loop over the input files. end
  2 comentarios
Tammy Chen
Tammy Chen el 29 de Ag. de 2016
Thanks for proofreading my rookie code in addition to answering my question. You gave a lot of good suggestions on how I should modify the order of functions and rename the variables, which helps tremendously. So I figured that putting only "values" in the second loop still runs into the same error with fprinf and cell array, so here's what I did:
for j=1:length(values{1,1})
pvt = values{1,1}
fprintf(fidO,'%s\t',pvt{j});
end
Now it's actually reading into the cell array. Since you don't have my Matlab workspace so you probably can't figure that there's still another layer to my values in order for "the curlies" to read into cell array strings.When I clicked on my "values" in Matlab workspace to display the data I want to copy, it shows the data in the following variable name "values{1,1}", which is the name of cell array that actually displays the data in each text file from my folder. I tried values {1} and it also works, but the point is, if your fprintf function still runs into error after using "curlies", there's probably another layer to your cell array that needs to be read into. Clicking on the relevant variables in work space helps. Here's my final product after modification.Thanks a bunch for the suggestions.
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)
dpb
dpb el 29 de Ag. de 2016
Ah, the issue is the strings instead of numerics...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by