0

I want to import string values from a .csv file and use them in MATLAB. I used readtable() and table2array functions in order to get an array of string values.

The csv file has 10 string values shown in below:

banana

apple

orange

lemon

apple

lemon

strawberry

apple

watermelon

orange

When I run my code I am supposed to an 1x10 array and it should have started with 'banana' but I get an 1x9 array and my first string is 'apple' not banana. In other words, I can't get the first value of the array. Can you help?

a = readtable('C:\Users\cinar\Desktop\Test Values.csv');
a = table2array(a);
4
  • You chose to read the .csv in as a table. Is the header of your table banana ? Commented Mar 21, 2019 at 20:23
  • Try the readmatrix command and see if this fixes your problem. Commented Mar 21, 2019 at 20:32
  • @medicine_man No, its just the first element. There isn't a header Commented Mar 21, 2019 at 20:34
  • 2
    Tables in Matlab have headers so readtable assumes that your CSV file has a header. Remove the semicolon on your first line of code and you'll see that it has used 'banana' as the header. readable is the wrong function. Try another function, e.g., dlmread or importdata or textscan. It's impossible to give you code without seeing your actual CSV file. Commented Mar 21, 2019 at 21:41

3 Answers 3

2

I changed my function for importing the data. I used importdata() function instead of readtable() function. When I write the following code, I get all the values.(1x10 array starting with banana)

a = importdata('C:\Users\cinar\Desktop\Test Values.csv');

Sign up to request clarification or add additional context in comments.

Comments

1

If you can use cell array I can recommend you this method:

a = fopen('C:\Users\cinar\Desktop\Test Values.csv');
data = fread(a, '*char')'; %read content
fclose(a);
results = regexp(data, ',', 'split'); %return cell array

The result is:

1×10 cell array

Columns 1 through 6

  {'banana'}    {' apple'}    {' orange'}    {' lemon'}    {' apple'}    {' lemon'}

Columns 7 through 10

  {' strawberry'}    {' apple'}    {' watermelon'}    {' orange'}

5 Comments

I didn't get the same result. I got 1x1 cell array, which has all the elements adjoint to each other in one cell
Do you checked data or results? The final, split is results 1x10 cell array. tested once again and it return what I shown.
I checked results and got 1x1 cell array, when I check data I get 1x83 char array
So what's the structure of your csv? You should have data 1x81 char and results should be 1x10 cell. Is your data real coma separated , or you placed it in column?
This method does not allow a newline character to split the data. Assuming the input is separated by newlines and not actually by commas like shown, that would explain this discrepancy.
1

In the home tab of the matlab, we have an option "import data", click it, and select the csv or excel file you are interested and you can either import the entire document or selectively import a particular column from the document! While importing, you can choose if you want the selected columns to be imported as "column vector/numeric matrix/ cell array..etc"... I use this because its easy to selectively import the rows and columns by just mouse clicks!

1 Comment

That's a way to get the data, but I want to get the data automatically by my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.