How do I read a tab delimited text file in MATLAB that has mostly numbers but sometimes has the word MAX in a few cells?

15,495

Solution 1

You can try textscan

C = textscan(fid, '%f', 'delimiter', '\t', 'treatAsEmpty', {'MAX'},'EmptyValue', 1500);

This will return a 1 by N cell vector with N being the number of values in the file. To transform it into a matrix, you can use reshape

C = reshape(cell2mat(C),[numOfRows numOfCols]);

Solution 2

http://www.mathworks.se/help/stats/tblread.html looks like a good function for this purpose with a simple usage:

data=tblread('data.tsv','\t')

Share:
15,495
krisharmas
Author by

krisharmas

Updated on June 17, 2022

Comments

  • krisharmas
    krisharmas almost 2 years

    I want to read in a range of cells from a tab delimited text file using MATLAB. This range of cells should be numbers, but recently the software that provides those numbers has put the word MAX in place of where the number should be if it is too big. This means that the range of cells that I am looking at now contains a few strings that say MAX and the rest are numerical values.

    I have been using dlmread until now to read this range of numerical values and it has been working fine. When there is a MAX there dlmread does not work.

    Here is the code I used to read the data:

    data = dlmread(filename, '\t', 20, 5);
    

    Here is the error:

    Mismatch between file and format string.
    Trouble reading number from file (row 152u, field 31u) ==> MAX
    MAX MAX 552.397949  33.415199   7.425600    3.379600
    6.6422090   \n
    

    Is there any way to read in this file without converting it into a .csv? I want to read in the file and change the cells that say MAX to a number like 1500.