Error: Error using load Unknown text on line number 1 of ASCII file cats.csv "Sex".

Hello everyone,
I am trying to load a csv. document on to Matlab but I'm struggling. MY csv file has three titles "Sex, Bwt and Hwt". The error I am getting is as below:
Error using load
Unknown text on line number 1 of ASCII file cats.csv
"Sex".
I tried to use readmatrix as well, but then I can't plot. I'm new to MATLAB and I have no idea what I am doing. Please help.
Best Regards

1 Comment

load() can only be used for csv files that contain no text, or only have text on lines that begin with %

Sign in to comment.

More Answers (1)

Try to save it as CSV instead of CSV UTF-8.

1 Comment

This is irrelevant in this case.
The problem is that the user had a csv file with a line of text headers. load() can only handle text headers in the restricted case that the first non-whitespace on each header line happens to be % . This is true regardless of whether the csv file is US-ASCII or UTF-8 .
filename = "cats.csv";
lines = ["Sex, Bwt, Hwt"; "M, 3.2, 11"; "F, 1.4, 14.2"];
writelines(lines, filename, 'Encoding', 'US-ASCII');
%verify contents
dbtype(filename)
1 Sex, Bwt, Hwt 2 M, 3.2, 11 3 F, 1.4, 14.2
%attempt load
try
data = load(filename);
fprintf('load US-ASCII worked!\n');
data
catch ME
fprintf('load US-ASCII failed!\n');
ME
end
load US-ASCII failed!
ME =
MException with properties: identifier: 'MATLAB:load:unknownText' message: 'Unknown text on line number 1 of ASCII file cats.csv↵"Sex".' cause: {} stack: [3×1 struct] Correction: []
%attempt readtable
try
data = readtable(filename);
fprintf('readtable US-ASCII worked!\n');
data
catch ME
fprintf('readtable US-ASCII failed!\n');
ME
end
readtable US-ASCII worked!
data = 2×3 table
Sex Bwt Hwt _____ ___ ____ {'M'} 3.2 11 {'F'} 1.4 14.2
writelines(lines, filename, 'Encoding', 'UTF-8');
%verify contents
dbtype(filename)
1 Sex, Bwt, Hwt 2 M, 3.2, 11 3 F, 1.4, 14.2
%attempt load
try
data = load(filename);
fprintf('load UTF-8 worked!\n');
data
catch ME
fprintf('load UTF-8 failed!\n');
ME
end
load UTF-8 failed!
ME =
MException with properties: identifier: 'MATLAB:load:unknownText' message: 'Unknown text on line number 1 of ASCII file cats.csv↵"Sex".' cause: {} stack: [3×1 struct] Correction: []
%attempt readtable
try
data = readtable(filename);
fprintf('readtable UTF-8 worked!\n');
data
catch ME
fprintf('readtable UTF-8 failed!\n');
ME
end
readtable UTF-8 worked!
data = 2×3 table
Sex Bwt Hwt _____ ___ ____ {'M'} 3.2 11 {'F'} 1.4 14.2
Notice the error from load() is the same in both cases, and both cases work fine for readtable() . The problem is that the file format is not suitable for load.

Sign in to comment.

Categories

Asked:

on 20 Oct 2019

Commented:

on 21 Aug 2023

Community Treasure Hunt

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

Start Hunting!