how to convert matrix contains numbers to matrix of strings

3 views (last 30 days)
Dear all,
I in my code I put my results in a matrix and sorted then extract the first fife columns in a new matrix which I want to convert to matrix contains strings, I found that in the command
A=[a,b,d,g,h]
L=regexprep(sprintf('%011.6f',A),'[.\L]','') , I got exactly the strings that i want but not sort
00117416770003913892002348335300007827780000071162
00282348210009411607005646964200018823210000171120
then after I sorted I tried to get the same strings in a matrix, but I got
L1 =
000209800034030390020482350000680608000006873
L1 =
00746770003938920023483353000078277800000762
I don't no why the {1} disappear the string should be exactly as L.
Thanks in advance

Accepted Answer

Geoff Hayes
Geoff Hayes on 12 Aug 2014
Imola - you seem to have two questions here. The first is why the L1 strings are not 1x50 (like the L in the previous loop), and how to store the data (presumably the L1) in a matrix.
In the call to regexp,
L1=regexprep(sprintf('%011.6f',BB(i,:)),'[.\L1]','')
the \L1 does not refer back to the variable name of L1. This was just a coincidence from the previous for loop where
L=regexprep(sprintf('%011.6f',A),'[.\L]','')
So just replace the L1 with L as follows
L1=regexprep(sprintf('%011.6f',BB(i,:)),'[.\L]','');
(Note - that it seems sufficient to have just '[.]' as the expression so I don't know why you have the '\L' as well.)
In order to save the data to a 4x50 matrix of characters, create the matrix before the for loop (pre-allocating enough memory) and then just copy the output from the regular expression to each row in the matrix as
sortedData = char(zeros(size(R,1),50));
for k=1:4
%regexprep replace string using regular expression
sortedData(k,:) = regexprep(sprintf('%011.6f',BB(k,:)),'[.\L]','');
end
Try the above and see what happens!

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!