Converting a character array which includes asterisks into a numeric in MATLAB

Viewed 385

I am struggling to convert character arrays containing asterisks ('*') into numeric doubles.

I have a cell array of character vectors based on data imported from a .dat file. For example, the cell array C contains a column of cells (e.g., C{1,1}, C{2,1}, ... C{n,1}), each of which containing a character vector, e.g., C{1,1} contains:

'23.000          *          *      1.000      1.000      1.000     34.000      5.065      6.719'

When I try to convert C{1,1} to a numeric double, MATLAB returns an empty double, e.g.,

new_double = str2num(C{1,1})

new_double =

     []

When I remove the asterisk manually, the code works:

 new_double = str2num(C{1,1})

 new_double =

   23.0000    1.0000    1.0000    1.0000   34.0000    5.0650    6.7190

All I want to do is read the data into a double array for further processing. I don't care if the command ignores the asterisks or replaces them with NaNs - the data with asterisks are not important to me. What is important is that I read data from the last two columns, e.g., 5.065 6.71. Unfortunately, I cannot index them since they are embedded within a character vector.

I have also tried using:

c2 = C{1,1};
new_double = sscanf(c2,'%f%'); 

But it stops reading at the asterisk, e.g.,

new_double =

    23

I have searched far and wide, the only useful post being: https://uk.mathworks.com/matlabcentral/answers/127847-how-to-read-csv-file-with-asterix However, I can't use this method because I am working from a character vector rather than delimited data.

2 Answers
Related