Search code examples
stringmatlabparsingnumbers

Reading a comma-separated string (not text file) in Matlab


I want to read a string in Matlab (not an external text file) which has numerical values separated by commas, such as

a = {'1,2,3'}

and I'd like to store it in a vector as numbers. Is there any function which does that? I only find processes and functions used to do that with text files.


Solution

  • I will use the eval function to "evaluate" the vector. If that is the structure, I will also use the cell2mat to get the '1,2,3' text (this can be approached by other methods too.

    % Generate the variable "a" that contains the "vector"
    a = {'1,2,3'};
    % Generate the vector using the eval function 
    myVector = eval(['[' cell2mat(a) ']']);
    

    Let me know if this solution works for you