I am having a .csv file which contains let’s say 50 rows.
At the beginning of each row I have a file name in the following format 001_02_03.bmp followed by values separated by commas. Something like this :
001_02_03.bmp,20,30,45,10,40,20
Can someone tell me how can I read the first column from the data?
I know how to obtain the data from the second column onward. I am using the csvread function like this X = csvread('filename.csv', 0, 1);. If I try to read the first column in the same manner it outputs an error, saying the csvread does not support string format.
Use
textscan, ie:A little more detail?
csvreadonly works with purely numeric data, so you can’t use it to get in data with a .bmp, or underscores for that matter. Thus we usetextscan. The funny looking format string input totextscanis just saying that the columns are, in order, of type string%s, then the next 6 columns are of type double%f%f%f%f%f%f(or you might choose to alter this to reflect an integer datatype – I personally rarely bother unless the quantity of data is huge or floating point precision is a problem).Note, if you just wanted to get the first column and ignore the rest, you can replace the format string with
%s% %*[^\n]. A final point, if your csv file has a header line, you can skip it using theHeaderLinesoptional input totextscan.