Issue when reading csv file using textscan()

조회 수: 7 (최근 30일)
Ahmad Aseeri
Ahmad Aseeri 2017년 4월 15일
답변: Image Analyst 2017년 4월 15일
I want to read dataset.csv in my program. I have the following code:
filename = 'dataset.csv';
delimiter = '\t';
formatSpec = '%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
fclose(fileID);
However, I get this error:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Can someone tell me the issue? I couldn't spot it!!
  댓글 수: 4
Ahmad Aseeri
Ahmad Aseeri 2017년 4월 15일
Thank you guys for responding. The dataset is in the same directory. I used the full path and got this error:
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> # Undirected graph: ../../data/output/lj.ungraph.txt\n
Star Strider
Star Strider 2017년 4월 15일
You need to look at the file in a text editor to see what it contains. Using textscan with the 'HeaderLines' name-value pair seems an attractive option.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2017년 4월 15일
You forgot to include the csv file so we're all going to be guessing here until you do. If you want a fast answer, you should attach the csv file so we can see what's wrong with it.
If the delimiter is a tab, then it's not a csv file despite the extension of csv that you incorrectly gave it. So you can't use csvread(). I suggest you try dlmread(). If you have headerlines, dlmread() now lets you specify how many header lines to skip by specifying the proper starting row and column.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by