how to convert matrix contains numbers to matrix of strings

조회 수: 4 (최근 30일)
imola
imola 2014년 8월 12일
편집: imola 2015년 2월 17일
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

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 8월 12일
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!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by