1

I've got an issue with matlab's new readmatrix function. For some reason, it has decided that my data file should be read as strings, and put into a cell array. The data are not strings, they are doubles, and what's more, I wrote them to file using writematrix, albeit using 'WriteMode','append'. The only tricky bit about it is that the rows aren't all the same length, but it handles other files with this same property perfectly fine, padding with nans.

How can I force readmatrix to recognise the data as doubles?

Output from opts = detectImportOptions('fileName.csv'):

opts = 

  DelimitedTextImportOptions with properties:

   Format Properties:
                    Delimiter: {','}
                   Whitespace: '\b\t '
                   LineEnding: {'\n'  '\r'  '\r\n'}
                 CommentStyle: {}
    ConsecutiveDelimitersRule: 'split'
        LeadingDelimitersRule: 'keep'
                EmptyLineRule: 'skip'
                     Encoding: 'UTF-8'

   Replacement Properties:
                  MissingRule: 'fill'
              ImportErrorRule: 'fill'
             ExtraColumnsRule: 'addvars'

   Variable Import Properties: Set types by name using setvartype
                VariableNames: {'x0_1'}
                VariableTypes: {'char'}
        SelectedVariableNames: {'x0_1'}
              VariableOptions: Show all 1 VariableOptions 
    Access VariableOptions sub-properties using setvaropts/getvaropts
        PreserveVariableNames: false

   Location Properties:
                    DataLines: [1 Inf]
            VariableNamesLine: 1
               RowNamesColumn: 0
            VariableUnitsLine: 0
     VariableDescriptionsLine: 0 
    To display a preview of the table, use preview

1 Answer 1

1

The relevant option here is VariableTypes. For some reason something in the file is triggering something in readmatrix to decide that it should be read in as chars. This can be fixed with setvartype.

opts = detectImportOptions('fileName.csv');
opts = setvartype(opts, 'x0_1', 'double');
data = readmatrix('fileName.csv',opts);

The string x0_1 is the automatic variable name matlab sets in VariableNames. Assuming it is possible to read the data as doubles, this produce a matrix of doubles.

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

Comments

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.