How to read data from csv files containing both text and data

254 visualizaciones (últimos 30 días)
I tried to use csvread but from the forum, it seems that I should use textscan to read the values when there is also text in the csv file: However, I don't understand what to put in argument of the function, especially when it comes to specifiers. I would like to have a matrix with my data. The .csv file is attached.
This is what I have tried so far. M = textscan('test.csv','%s %s %s %s')
I work on MATLAB 2014a

Respuesta aceptada

dpb
dpb el 16 de Abr. de 2018

'Pends on what you mean, specifically, by 'data'. One of the easiest ways to treat mixed data files if you do want the numeric and text portions separately is xlsread; it will autogmagically return the text, numeric and then the 'raw' data as a cell array...

>> [n,t,r]=xlsread('test.csv')
n =
     1     1     1
     2     2     2
     3     3     3
     4     4     4
     5     5     5
     6     6     6
     7     7     7
     8     8     8
     9     9     9
    10    10    10
    11    11    11
t =
  15×4 cell array
    'text'    'text'    'text'    'text'
    'text'    'text'    'text'    'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    ''        ''        ''        'text'
    'text'    'text'    'text'    'text'
    'text'    'text'    'text'    'text'
r =
  15×4 cell array
    'text'    'text'    'text'    'text'
    'text'    'text'    'text'    'text'
    [   1]    [   1]    [   1]    'text'
    [   2]    [   2]    [   2]    'text'
    [   3]    [   3]    [   3]    'text'
    [   4]    [   4]    [   4]    'text'
    [   5]    [   5]    [   5]    'text'
    [   6]    [   6]    [   6]    'text'
    [   7]    [   7]    [   7]    'text'
    [   8]    [   8]    [   8]    'text'
    [   9]    [   9]    [   9]    'text'
    [  10]    [  10]    [  10]    'text'
    [  11]    [  11]    [  11]    'text'
    'text'    'text'    'text'    'text'
    'text'    'text'    'text'    'text'
>> 

readtable is also useful; it will bring in all the data as columnar but will be cell array since each column is a mixture.

Truthfully, depending upon what it is that is to be done, it might realistically be the best thing to reorganize the file structure and "fix" the problem at that point.

  1 comentario
Patrick F
Patrick F el 16 de Abr. de 2018
Thanks, It works very well. it is much more simple than the strategy employing textscan that I finally discovered. I will use your idea. Thanks.

Iniciar sesión para comentar.

Más respuestas (2)

Patrick F
Patrick F el 16 de Abr. de 2018

I resolved my problem. Here is the solution:

First we care about the first to rows which contain only text. 1. We declare and open the file

   fileID = fopen('test.csv');

2. We read two rows which contains 4 columns of text

    formatSpec = '%s';
    N = 4;
    C_text1 = textscan(fileID,formatSpec,N,'delimiter',',');
    C_text2 = textscan(fileID,formatSpec,N,'delimiter',',');

3. Then we read the lines containing data (3 columns) and a string(last column)

C_data = textscan(fileID,'%f32 %f32 %f32 %s','delimiter',',');
fclose(fileID);

4. We concatenate the data into the matrix we are interested in.

M_data = [C_data{1} C_data{2} C_data{3}]

5. Execute and it works!

M_data =
     1     1     1
     2     2     2
     3     3     3
     4     4     4
     5     5     5
     6     6     6
     7     7     7
     8     8     8
     9     9     9
    10    10    10
    11    11    11

Sarah Palfreyman
Sarah Palfreyman el 30 de Abr. de 2018
You can also use Text Analytics Toolbox for this workflow.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by