How To Get Number Of Columns in Non-Rectangular Array of Data for textscan Function
Mostrar comentarios más antiguos
Hello,
I am currently using the following line of code for extracting data from a csv file, which contains non-rectangular array of data
C = textscan(fileID, repmat('%f',[1 22]), Delimiter, ',', 'EmptyValue', NaN);
What I would love to do is replace that 22 with a variable. Right now I need to check the csv, count the columns (currently, 22) and enter that number manually. Is there a way I could automate that and replace 22 with a variable, eliminating the need to look in the csv file in the future?
Thanks
Respuestas (1)
Star Strider
el 19 de Abr. de 2017
0 votos
Since your files don’t appear to have header lines, the xlsread or csvread functions might be more appropriate than textscan to import your files.
4 comentarios
EvanThomas
el 19 de Abr. de 2017
Editada: EvanThomas
el 19 de Abr. de 2017
Star Strider
el 19 de Abr. de 2017
My impression is that they should be able to. I’ve not tried them with your files, so I don’t know how the functions would work with them.
Also, textscan may allow you to read beyond the column limits. When I’ve done that, it simply filled the fields with no data as empty cells (although it is best to fill them with NaN values as you have instructed it to do, since you can then use cell2mat to convert them to numeric matrices). So you may be able to use textscan without alteration, then parse the cell arrays it returns afterwards.
It’s probably best to experiment to see what functions work best with your data. I would try them all on a few representative files to see what the results are.
EvanThomas
el 19 de Abr. de 2017
Editada: EvanThomas
el 19 de Abr. de 2017
Star Strider
el 19 de Abr. de 2017
My pleasure.
I’ve found textscan to be rather robust to (my) stretching it beyond its intended limits. In any event, you can always comment your code (good practice anyway) to check your textscan call if your code starts behaving strangely.
Categorías
Más información sobre Large Files and Big Data en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!