How to separate characters from words and write both of them to matrice when reading from text file
Ältere Kommentare anzeigen
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 Kommentare
per isakson
am 25 Nov. 2014
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?
Tural
am 25 Nov. 2014
Antworten (2)
Geoff Hayes
am 25 Nov. 2014
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' ')' ';'
per isakson
am 23 Dez. 2014
A construct based on regexp:
>> A{1} = regexp( ',considering);', '\W|\w++', 'match' );
>> A{1}{2}
ans =
considering
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!