Loading mixed numeric and string data

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.


-------------Problems Reply------------

In cases where you absolutely don't know how to import your data, just use the Import Data GUI and generate a script. Loading mixed numeric and string data

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.

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

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

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

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

Category:matlab Views:3 Time:2018-11-08
Tags: matlab file io

Related post

  • Associative array with mixed (numerical and string) indices? 2010-06-28

    How would one implement a dynamic associative array that could take any number of mixed indices (integers, strings, or both)? I aim to simulate structures by providing, for example, people[3].location as syntactical sugar for people[3, "location"]. H

  • How should I model a field that can contain both numeric and string values in SQL Server 2005? 2008-08-29

    I have a new database table I need to create... It logically contains an ID, a name, and a "value." That value field could be either numeric or a character string in nature. I don't think I want to just make the field a varchar, because I also want t

  • Is there a generic way to do comparison both numeric and string in Perl? 2011-07-13

    As everyone knows, the numeric's comparison uses different operators to string's. So Is there a generic way to do comparison both numeric and string in Perl ? Should I always check the value is numeric or string before doing the comparison? I am a fr

  • Image and string data in an ajax call? 2011-11-02

    I want to have one query that will pull out image and string data out of my table in one call and send that information back to the server via an ajax call. I am using php as a back end. I guess that would bring up two questions. Since I am using php

  • Evaluating numerical and text data and returning a value using drowpdown lists 2012-05-27

    I need a function that will lookup in a table made up of numerical and text data based on an input selected by the user in a dropdown list. I can't use a vlookup because some of the table headers are to similar in name. So I chose to use sumproduct.

  • Sorting mixed numbers and strings 2009-06-23

    I have a list of strings that can contain a letter or a string representation of an int (max 2 digits). They need to be sorted either alphabetically or (when it is actually an int) on the numerical value it represents. Example: IList<string> in

  • LoaderCallbacks.onLoadFinished is not called when loader is resused and contains data 2011-08-15

    I have 1 activity and 2 fragments. Both fragments use a custom AsyncTaskLoader to get some data from a webservice and as i'm using a Loader it should keep the data across activity and fragment re-creations. Both fragments override the onActivityCreat

  • How do I find the count of concatenated columns of int and string data types in SQL CE? 2012-04-29

    How to merge different data type of column on SQL? I have a table: +-----------+-------------+ + Column + Data Type + +-----------+-------------+ + Day + Int + + Time + Varchar + + Quota + Int + +-----------+-------------+ I want to merge all of the

  • send audio file and string data to server from android 2011-09-01

    I want to send audio file and some string data to server from android and I am using the same as given in this link (upload an image and audio in One request in android), but I am getting the error where I am adding the data. entity.addPart("title",n

  • How to load XML document and Query data on user selection 2012-02-13

    Hope you are doing well and happy with your life whatever you are doing. I have a big question. Well, it's a big job for me. I tried to google and follow some tutorials frist but finally realise I need help from stackoverflowers. I have a one big xml

  • When I download excel and open it with office for Mac excel it looks like a load of code and no data. Why? 2014-03-30

    When I download an excel file from my pc and try to open it on my MacBook, it looks like a heap of unformatted code. It doesn't appear to contain any useful data. Why is this happening? --------------Solutions------------- What email Client? SeaMonke

  • Fscanf file read in matlab for mixed numeric and non-numeric data 2013-04-08

    I try to read data from file but I have no idea how to use fscanf normally. Have file like this surname1 A.A.(16 char everytime) 23323 3232 232322 .... surname2 A.A. 23322 3232 232322 .... surname3 A.A. 23322 3232 232322 .... How to use fscanf here?

  • Best hash function for mixed numeric and literal identifiers 2009-12-14

    For performance reasons I have a need to split a set of objects identified by a string into groups. Objects may be either identified by a number or by a string in prefixed (qualified) form with dots separating parts of the identifier: 12 323 12343 23

  • ANT: Load file names and extract data from the file names 2011-09-08

    Hello I wasn't quite sure how to title this question, but I'll explain why I'm trying to do. First of all, I have a folder that contains SQL scripts in a specific format, which is updateXtoY.sql, where X and Y are integers. What I need is to know whi

  • regarding saving the numerical and string values altogether 2011-11-30

    I have a string matrix with N rows and 2 columns. Each cell stores a string. I have another N*1 vector, where each entry is a numerical value. How can I save these two structures into a single text file row by row. In other words, each row of the sav

  • Using xlsread in MATLAB to read number and string data 2012-04-14

    I have a large column of data in Excel that I'd like to read into a string cell array. However, some of the entries are numbers, and the rest are strings. So I have something like 288537 288537 312857 589889 589889 1019503 1019503 1098802 1098802 abc

  • What classification algorithm can handle numeric and nominal data 2013-06-14

    This is probably a newbie question on possible classification algorithm, so please bear with me. I have a dataset that comprises both nominal and numeric attribute which may look the example below (not actual dataset). What kind of algorithm would be

  • Issue with jQuery UI Tabs (loaded via ajax) and form data 2011-02-23

    I am attempting to set up an interface that has a form containing filters about some jQuery UI Tabs. The tabs are loaded via ajax. When I click on one of the tabs, I want the data from the form to be submitted to that tab. I set up the ajaxOptions to

  • string::c_str() and string::data() not working for conversion to const char* 2011-07-27

    I have a map structure in a class that is defined as std::map<const char*, double>, and in the constructor, I add three elements all set to zero, with they keys "MR_WP", "MR_WM", and "MR_WC". I also have several access functions to the map, but

  • Making mixed numeric and alpha entry easier for mobile browers 2009-01-15

    I'm refurbishing a simple web-app that our field techs use. One of my goals is to make it Blackberry-friendly, as all of the techs are now using those. This app accepts basic information about a job and stores it. Among that info are times, telephone

  • Difference between numeric and string value in PHP 2010-05-10

    I am getting a difference when comparing two string which are 0 and '0' in PHP. Can I do anything to make them be compared equally in an if action? Thanks! --------------Solutions------------- If you compare using: if ('0' == 0) // if '0' is equal to

  • Add numbers and strings (data from UITextFields) 2012-01-03

    I have several UITextFields and the principle of my app is to add them together. The user can type either numbers or words into the UITextField. I haven't got problems with the number calculations, it's calculating with the words. My question is: if

  • zillow.com not loading pics, charts and most data! 2012-07-21

    does anyone else have this problem? it used to work, what happened? thanks. --------------Solutions------------- Hi, 1. Do you get any error message? 2. Are you aware of making any changes to the computer prior to the issue? Method 1: Perform the ste

  • Denied access to folders after loading Windows 7 and transferring data and I am the Administrator. 2013-12-12

    After installing Windows 7 and transferring everything from my old computer I have a folder named Documents and Settings. When I try to open it I get a "You don't have permissions to access folder". I am the administrator. If I click on "click her fo

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.108 (s). 11 q(s)