Error using textscan when I try to plot from text file
2 次查看(过去 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.
0 个评论
回答(2 个)
KSSV
2021-2-15
data = importdata('test50000.txt') ;
data is a array of dimensions 12371x23. Next what?
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 个评论
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 Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!