MATLAB Answers

0

Reading Text and Parsing by Character

Hi, I need to somehow read in a text file with no standard delimiters (spaces,tabs,ect.) and parse it by character into a long single row array.
For example,
rand.txt = "thequickbrownfoxjumpsoverthelazydog"
finalarray = ['t' 'h' 'e' 'q' 'u' 'i' 'c' 'k'....]
Any advice??
Thanks in advance!

  4 Comments

Show 1 older comment
Sorry I was in a rush, what I meant was that I will have a text file that contains a line like "thequickbrownfoxjumpsoverthelazydog"
I want to create a string array that looks like ['t' 'h' 'e' 'q' 'u' 'i' 'c' 'k'....] where the input string is parsed by each character.
[] is the horizontal concatenation operator. ['a' 'b'] has exactly the same result as 'ab' . Are you looking for a cell array of strings? {'a' 'b'} ?

Sign in to comment.

2 Answers

Answer by Albert Yam
on 30 Jul 2012
Edited by Albert Yam
on 30 Jul 2012
 Accepted Answer

rand.txt = 'thequickbrownfoxjumpsoverthelazydog';
finalarray = regexp(rand.txt,'[a-z]','match')
Edit: if every character, including periods and such
finalarray = regexp(rand.txt,'.','match')

  0 Comments

Sign in to comment.


Answer by Walter Roberson
on 30 Jul 2012

finalarray = num2cell(rand.txt);

  1 Comment

this works great too

Sign in to comment.