How To Get Number Of Columns in Non-Rectangular Array of Data for textscan Function

2 vues (au cours des 30 derniers jours)
EvanThomas
EvanThomas le 19 Avr 2017
Commenté : Star Strider le 19 Avr 2017
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

Réponses (1)

Star Strider
Star Strider le 19 Avr 2017
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 commentaires
EvanThomas
EvanThomas le 19 Avr 2017
Modifié(e) : EvanThomas le 19 Avr 2017
Thanks StarStrider, I had tried csvread already (my go-to import function on most occasions), but it would not work on this data set. Each column is a different length, so it didn't like it. still, I was hoping for a slick way around that. Not really seeing one, likewise with xlsread. textscan is working perfectly right now, I just know sooner, or later, the number of columns will change, and that hardcoded "22" is going to bite me, while I sit and wonder for a while why my script broke because I had since forgotten about it, hehe
Star Strider
Star Strider le 19 Avr 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by