How to separate characters from words and write both of them to matrice when reading from text file

example I have: ",considering);" I want this like
A{1}{1} = ,
A{1}{2} = considering
A{1}{3} = )
A{1}{4} = ;
note: there is no white space between characters and words

2 Comments

I assume that you look for a code that will handle more strings than this example. What is the rule according to which the input string shall be split?
actually I have to split text so non-letter characters will be stored in its cell ,when I do textscan (considering) is word considering stored with brackets

Sign in to comment.

Answers (2)

Tural - you could try using regexp to split the string into its letter and non-letter parts, splitting on the non-letters. Something like
str = ',considering);';
[A, nonLetterIdcs] = regexp(str,'[^A-Za-z]','split');
In the above, we split the str message on those characters that are not (the ^ means "not") in the square bracket expression (so we split on anything that is not in the range A-Z or a-z), and our output becomes
A =
'' 'considering' '' ''
nonLetterIdcs =
1 13 14
A is a cell array, and nonLetterIdcs is an array of indices of the non-letters that we have split on. Now we need to combine the two as
A(cellfun(@(x)isempty(x),A)) = cellstr(str(nonLetterIdcs)');
We use cellfun to look for the empty strings of A which correspond to the positions in the original str that have non-letters. And we need to "fill" these empty strings with the corresponding non-letter from str, using the nonLetterIdcs. Having done that, A becomes
A =
',' 'considering' ')' ';'
A construct based on regexp:
>> A{1} = regexp( ',considering);', '\W|\w++', 'match' );
>> A{1}{2}
ans =
considering

Categories

Asked:

on 25 Nov 2014

Answered:

on 23 Dec 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!