Error using textscan when I try to plot from text file
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
KSSV
le 15 Fév 2021
data = importdata('test50000.txt') ;
data is a array of dimensions 12371x23. Next what?
Walter Roberson
le 15 Fév 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 commentaires
Walter Roberson
le 16 Fév 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 [
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!