Loading mixed numeric and string data

928 Views Asked by At

I'm new to matlab. I have the a file named iris.data and I'm trying to load it's contents into variables. The file have the folowing contents:

5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa

I tried:

load iris.data

But I got:

Error using load
Unknown text on line number 1 of ASCII file iris.data
"Iris-setosa". 

why it's giving me this error, or I'm totally went on the wrong direction, and there is a better way to do it.

Thanks!!

2

There are 2 best solutions below

0
On BEST ANSWER

In cases where you absolutely don't know how to import your data, just use the Import Data GUI and generate a script. enter image description here

This is what you get for your case:

%% Initialize variables.
filename = 'C:\Users\Robert Seifert\Desktop\SO\data.txt';
delimiter = ',';

%% Format string for each line of text:
%   column1: double (%f)
%   column2: double (%f)
%   column3: double (%f)
%   column4: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%*s%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,  'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.

%% Create output variable
data = [dataArray{1:end-1}];
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;

and you get:

data =

    5.1000    3.5000    1.4000    0.2000
    4.9000    3.0000    1.4000    0.2000
    4.7000    3.2000    1.3000    0.2000
    4.6000    3.1000    1.5000    0.2000
    5.0000    3.6000    1.4000    0.2000
    5.4000    3.9000    1.7000    0.4000
    4.6000    3.4000    1.4000    0.3000
    5.0000    3.4000    1.5000    0.2000
    4.4000    2.9000    1.4000    0.2000
    4.9000    3.1000    1.5000    0.1000
    5.4000    3.7000    1.5000    0.2000
    4.8000    3.4000    1.6000    0.2000
    4.8000    3.0000    1.4000    0.1000

The scripts you get are really well commented and you can apply it on almost any case and learn ;)

0
On

To read the contents of a file, you can use:

filename = 'filename.extension';
fileID = fopen(filename);
fileData = fread(fileID);
fclose(fileID);

The data is now in the fileData variable in your workspace. To get the data in a string, use fileStr = char(fileData)'