S.m
S.m

Reputation: 77

csvread function loads 0 values

I am trying to load data from a csv file using the function csvread in Matlab as follows:

data=  csvread('2017.socc.dataset.csv');

label = data(:,1);
data=data(:,2:8)

The file.csv contains numerical values, but when I load it I get the variable data with 0 values. The first rows of the file are shown in figure:  the 2017.socc.dataset.csv

The variable data:values in the variable data

How can I solve it?

Upvotes: 1

Views: 456

Answers (2)

Nick J
Nick J

Reputation: 1610

Just to close the issue: it appears that csvread is working correctly. What you are seeing is an artifact of your display setting in Matlab. Because your data spans over 10 orders of magnitude, Matlab normalizes the display by the largest value (in this case dividing the values by 1e9) and then only displays 5 digits of value. Despite zeros being displayed, the actual values should be stored in the variable. E.g., if you queried the first value of data you should get:

>> data(1)
ans = 
  436.0625

The key is in the first part of the array output:

data =
   1.0e+09 *
   ...

In scientific notation, 436.0625 is 0.0000004360625e+09. So when only showing 5 digits, it only shows the leading zeros. As suggested above, changing your display using format long will show you more precision in the display.

>> format long
>> data
data =
   1.0e+09 *
   0.000000436062500   0.000000193426604   0.000000194667969   0.000000000617521   0.000000000593853   0.000000003977791   0.329294135000000
   ...

Note that even with format long, if any of your values are smaller than 1e-6 it will display as 0.000000000000000, even thought Matlab has stored the actual value.

for more data display options you can see the Matlab help page for 'format'

Upvotes: 2

tyogi
tyogi

Reputation: 650

An actual excerpt of the file (rather than a screenshot) would help as this is likely some sort of encoding issue (CSVs have a number of ways they can be encoded, depending on localization for example).

However, for all data creation purposes, I always find that the Matlab GUI does a great job, and if you want the code corresponding to the import it can be generated as well!

Go to Home / Import data, then just follow the steps until the preview enables you to have the data the way you want it (NB: for each column, you can choose a preferred type). Then, you can either click on "import selection", which puts the variable in the workspace, or choose "generate script" or "generate function" to get the actual code.

== edit == I hadn't seen the output you were getting, and the comment above (which was posted while I was answering) regarding the long format is likely the correct solution in this case, if the actual values aren't zeros (which is what I had assumed from your question).

Upvotes: -1

Related Questions