Error: Error using load Unknown text on line number 1 of ASCII file cats.csv "Sex".
Show older comments
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
Walter Roberson
on 20 Oct 2019
load() can only be used for csv files that contain no text, or only have text on lines that begin with %
Accepted Answer
More Answers (1)
Farhad
on 22 Jun 2023
0 votes
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)
%attempt load
try
data = load(filename);
fprintf('load US-ASCII worked!\n');
data
catch ME
fprintf('load US-ASCII failed!\n');
ME
end
%attempt readtable
try
data = readtable(filename);
fprintf('readtable US-ASCII worked!\n');
data
catch ME
fprintf('readtable US-ASCII failed!\n');
ME
end
writelines(lines, filename, 'Encoding', 'UTF-8');
%verify contents
dbtype(filename)
%attempt load
try
data = load(filename);
fprintf('load UTF-8 worked!\n');
data
catch ME
fprintf('load UTF-8 failed!\n');
ME
end
%attempt readtable
try
data = readtable(filename);
fprintf('readtable UTF-8 worked!\n');
data
catch ME
fprintf('readtable UTF-8 failed!\n');
ME
end
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.
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!