Minoru
Minoru

Reputation: 1730

MATLAB - Need to make a cell array from a text column file

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);

Upvotes: 2

Views: 82

Answers (2)

Ryan J. Smith
Ryan J. Smith

Reputation: 1140

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

Upvotes: 1

il_raffa
il_raffa

Reputation: 5190

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.

Upvotes: 0

Related Questions