Error using textscan when I try to plot from text file

3 次查看(过去 30 天)
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 [

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by