Prasad
Prasad

Reputation: 6034

Read multi format data in Octave

I have just started learning Octave and am trying to read a csv file comprising of data in the form of string, integer and float. Example is shown below

a,b,c,d
1,c,10,1234.2
e,2,4,5

I initially tried a lot using csvread. Some of my examples are below:

[val1, val2, val3, val4] = csvread('input.csv', '%s %s %s %s');

But I am getting an error like error: dlmread: error parsing RANGE

Then using this question, I made use of textread function as shown below:

[val1, val2, val3, val4] = textread('input.csv', '%s %s %s %s', 'delimiter', ',');

I am able to read the data now, but when I print the values, I am even getting the address of the values like shown below.

val1 = 
{
  [1,1] = a
  [2,1] = 1
  [3,1] = e
}

Please can somebody help me in

1) Finding out what is wrong in csvread.

2) Why textread function is returning addresses. How to avoid them?

Thanks in advance for the help.

Upvotes: 1

Views: 849

Answers (1)

Žiga Sajovic
Žiga Sajovic

Reputation: 324

The use of dlmread, which is the cause of your error, can be found in this answer. Note that

x = csvread (filename, dlm_opts)

is equivalent to

x = dlmread (filename, "," , …)

The item returned by textread

val1 = 
{
  [1,1] = a
  [2,1] = 1
  [3,1] = e
}

is a cell array of strings. Cell arrays are used, as a matrix must be composed of vectors of equal length, which is not the case, when one operates one words of variable size. If one would have stored words in a matrix, all rows/columns would have had to be "padded", to be as long as the longest word stored.

To convert an element at a specific index of a cell array (val1) to a vector you employ cell2mat

vec=cell2mat(val1(index));

Upvotes: 2

Related Questions