Error using textscan when I try to plot from text file

3 views (last 30 days)
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.

Answers (2)

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

Sign in to comment.


Walter Roberson
Walter Roberson on 15 Feb 2021
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 Comments
LuYao Guo
LuYao Guo on 16 Feb 2021
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 on 16 Feb 2021
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 [

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!