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

조회 수: 210 (최근 30일)
Patrick F
Patrick F 2018년 4월 16일
답변: Sarah Palfreyman 2018년 4월 30일
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

채택된 답변

dpb
dpb 2018년 4월 16일

'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
Patrick F
Patrick F 2018년 4월 16일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Patrick F
Patrick F 2018년 4월 16일

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 2018년 4월 30일
You can also use Text Analytics Toolbox for this workflow.

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by