matlab - Loading mixed numeric and string data -
i'm new matlab. have file named iris.data , i'm trying load it's contents variables. file have 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 got:
error using load unknown text on line number 1 of ascii file iris.data "iris-setosa".
why it's giving me error, or i'm totally went on wrong direction, , there better way it.
thanks!!
in cases absolutely don't know how import data, use import data gui , generate script.
this case:
%% initialize variables. filename = 'c:\users\robert seifert\desktop\so\data.txt'; delimiter = ','; %% format string each line of text: % column1: double (%f) % column2: double (%f) % column3: double (%f) % column4: double (%f) % more information, see textscan documentation. formatspec = '%f%f%f%f%*s%[^\n\r]'; %% open text file. fileid = fopen(filename,'r'); %% read columns of data according format string. % call based on structure of file used generate % code. if error occurs different file, try regenerating code % import tool. dataarray = textscan(fileid, formatspec, 'delimiter', delimiter, 'returnonerror', false); %% close text file. fclose(fileid); %% post processing unimportable data. % no unimportable data rules applied during import, no post % processing code included. generate code works % unimportable data, select unimportable cells in file , regenerate % script. %% create output variable data = [dataarray{1:end-1}]; %% clear temporary variables clearvars filename delimiter formatspec fileid dataarray ans;
and 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 commented , can apply on case , learn ;)
Comments
Post a Comment