필터 지우기
필터 지우기

Error using textscan when I try to plot from text file

조회 수: 6 (최근 30일)
LuYao Guo
LuYao Guo 2021년 2월 15일
댓글: Walter Roberson 2021년 2월 16일
Hi,
I'm trying to plot two columns which are channel 1 and 8 from a text file using textscan, but I get an errow showing:
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 2) ==>
,-180891.796875,187500.000000,-181084.218750,-181090.500000,-181012.625000,-180932.765625,-28428.736328,-181035.890625,0.632000,0.356000,0.726000,192.000000,0.000000,0.000000,0.000000,0.000000,0.00000...
And my code is look like following:
%% Initialize variables.
filename = 'test50000.txt';
delimiter = '\n';
%% Format for each line of text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%%f%f%f%f%f%f%f[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%% Close the text file.
fclose(fileID);
%% Create output variable
X = dataArray(:,1)
Y = dataArray{:,8};
xlabel('X');
ylabel('Y');
Can anyone please help? Thank you very much.

답변 (2개)

KSSV
KSSV 2021년 2월 15일
data = importdata('test50000.txt') ;
data is a array of dimensions 12371x23. Next what?
  댓글 수: 1
LuYao Guo
LuYao Guo 2021년 2월 15일
Hi, then I want to plot the first 255 rows, where column 1 as x axis and column 8 as y axis. Cheers.

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


Walter Roberson
Walter Roberson 2021년 2월 15일
delimiter = '\n';
Newline
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
The newline is passed in as the 'Delimiter' option, so textscan now expects whitespace or this passed-in newline as marking the boundary between columns of data, except for literal characters explicitly inserted into the format.
formatSpec = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%%f%f%f%f%f%f%f[^\n\r]';
No literal characters in the format specification, so just whitespace and that newline are expected between fields.
,-180891.796875,187500.000000,-181084.218750,-181090.500000,-181012.625000,-180932.765625,-28428.736328,-181035.890625,0.632000,0.356000,0.726000,192.000000,0.000000,0.000000,0.000000,0.000000,0.00000...
Notice the leading comma. textscan() was trying to read the comma at the time the problem occurred. Reading the first column with %f terminated at the comma because comma is not part of a number, leaving textscan positioned at the comma. Is comma whitespace? No. Is comma the delimiter supplied? No. Does comma appear literally in the input format at this point? No. Can comma be matched by the next format specification, the (second) %f ? No. So comma is invalid on input at this point, and textscan gave up.
You could set error processing options to cause it to continue, but it is clear you would be unlikely to get the desired results if you did that.
So what should you do? Well, either change that delimiter = to ',' (comma) or else change the formatSpec to have literal commas at each relevant point.
  댓글 수: 2
LuYao Guo
LuYao Guo 2021년 2월 16일
Hi Sir, thanks for answering. So based on your suggestions, I change the delimiter to:
delimiter = ',';
then I get error showing like:
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Literal' field from file (row number 1, field number 17) ==>
0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,1612839358.587550\n
Error in TEST1 (line 14)
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines'
,1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
How could I solve this though? Sorry this question might be silly but I'm quite blank about this.
Walter Roberson
Walter Roberson 2021년 2월 16일
formatSpec = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%%f%f%f%f%f%f%f[^\n\r]';
After the 16th %f you have %%f
Also you need a % before the [

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

카테고리

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