Sean
Sean

Reputation: 1

Text to array conversion

How do you convert a text file which has commas into an array delimited by those commas? For example, I use

var = importdata(filename)
disp(var) 

This obviously displays the contents which are 'please, help, me'

How do I then get var to be an array such that I could extract a single word using something similar to var(2)?

Upvotes: 0

Views: 47

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

Use strsplit (requires ≥ R2013a) or split (requires ≥ R2016b) to split the character array var{1} into a cell array that contains those words at its separate cells.

v = strsplit(var{1},', ');   %or v = split(var{1},' ,');

Now v{1}, v{2} and v{3} give 'please', 'help' and 'me' respectively.
var{1} is used since you must be returned a cell array var from importdata. If var was not a cell array but a character array then you would not be getting single quotes in the output of disp.

Upvotes: 2

Related Questions