Using fscanf does not output a two-dimensional array

조회 수: 6 (최근 30일)
Ryoma Molnar
Ryoma Molnar 2019년 8월 9일
답변: Sai Bhargav Avula 2019년 8월 12일
I would like to my data stored in a text file (Bmatrixu8.txt) converted to an size [Inf, 3] array.
While this should be easy, my code only seems to output an empty array.
The only thing I could see going wrong is is an incorrect 'formatSpec', but I don't notice anything wrong with it.
Just in case this is relevant, I was having issues earlier with the encoding of my text file. Because it was in Unicode, whitespace appeared in between every character. This was fixed by changing the encoding to UTF-8.
Any ideas on what could be going on?
clc; clear;
fileID = fopen('Bmatrixu8.txt','r');
A = fscanf(fileID,'%d,%d,%d\n');
fclose(fileID);
  댓글 수: 1
Stephen23
Stephen23 2019년 8월 9일
편집: Stephen23 2019년 8월 9일
407,-4754,1992
-214,--212,-6273,3147 % <- How do you expect this line to be parsed ?
1130,-8295,3653
"This was fixed by changing the encoding to UTF-8."
Better to simply specify the file encoding.

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

답변 (1개)

Sai Bhargav Avula
Sai Bhargav Avula 2019년 8월 12일
As mentioned by Stephen Cobeldick the text file has 4 elements in the 353th line and cannot be parsed. Also, try using the textscan() function which provides better flexibility over the formatted data and handling the delimiters and empty value conversions.
Refer the following link for better understanding of the textscan function.
Hope this helps !

카테고리

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