Home > front end >  Reading a comma-separated string (not text file) in Matlab
Reading a comma-separated string (not text file) in Matlab

Time:10-28

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. Thank you

CodePudding user response:

I think you're looking for sscanf

A = sscanf(str,formatSpec) reads data from str, converts it according to the format specified by formatSpec, and returns the results in an array. str is either a character array or a string scalar.

CodePudding user response:

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

  • Related