2

I have a database from UCI Machine Learning (Abalone Database)and I need to separate the first column, which is a character, from the other columns, which are double.

The second part I already have with this code:

abaloneData = csvread('abalone.data',0,1);

I tried a lot to gatter the first part to use on KNN, but I failed every time.

Thanks.

EDIT1:

read_data.m

function [features, labels] = read_data()
    features = csvread('abalone.data',0,1);

    fileID = fopen('abalone.data');
    data = textscan(fileID,'%s %*[^\n]', 'Delimiter',',');
    fclose(fileID);
    labels = cell2mat(data{1});
end

knn.m

[features,labels] = read_data();

Mdl = fitcknn(features,labels);
1
  • This is done with textscan Commented May 10, 2015 at 21:01

2 Answers 2

1

The problem, as you've probably realized by now, is that csvread() only works for numeric values. Instead you need to use textscan() to deal with strings / characters. Try this:

fileID = fopen('abalone.data');
data = textscan(fileID,'%s %*[^\n]', 'Delimiter',',');
fclose(fileID);
labels = cell2mat(data{1});

This will open the file and read in the first column as a string, skipping the remaining elements in each row. Finally, this data gets converted from cell to a char vector called labels

Sign up to request clarification or add additional context in comments.

8 Comments

Your code gives me an array of char, but when I try to use the KNN, I get this error: ??? Undefined function or method 'fitcknn' for input arguments of type 'double'.. Do you know what's the problem? I'll edit the question to add the entire code.
Your edited code works fine for me. fitcknn() is a function belonging to the statistics toolbox. Are you sure you have access to this toolbox?
I am. If you want, I can print the help response.
In your matlab command line, execute ver. Double check that you see Statistics Toolbox in the list that is returned. You can check further with help fitcknn to ensure you have access to that function.
Downloaded the last version of Matlab and it worked great! Thanks, @Ryan!
|
0

You can try using textscan if the number of columns is the same for each row:

fp=fopen('f.txt','rt')
a=textscan(fp,'%c%f%f%f%f%f%f%f%f','delimiter',',')
fclose(fp)

the cellarray contains the data stored in the file.

Its size should be (1xn) where "n" is the number of columns of the file.

1 Comment

This one returns an array with all the text elements. I need only the first column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.