필터 지우기
필터 지우기

Speed up string Assembly

조회 수: 1 (최근 30일)
Jonas Widmer
Jonas Widmer 2018년 4월 14일
답변: Jonas Widmer 2018년 4월 15일
Hi all,
Question: I try to assamble strings with the following loop (ch_empty10 = ' ', and db_Value(i) = e.g. 4.1563):
for i = 1:100000
ch_String = [ch_String, ch_empty10(1:10-length(num2str(db_Value(i)))),num2str(db_Value(i))] %(-> would result in ' 4.1563')
end
In my application I have >100'000 different db_Values and all need to be assembled the same way and are combined to one string. Is there somehow a way that I can avoid using a loop that goes thru all nodes to speed up the process.
I hope I formulated the question clear enough. Thanks for your help! Best, J

채택된 답변

Jonas Widmer
Jonas Widmer 2018년 4월 15일
Ah, think I found the solution:
ch_String = sprintf('%10.4f%10.4f%10.4f\r\n', [my_Matrix]')

추가 답변 (2개)

Walter Roberson
Walter Roberson 2018년 4월 15일
편집: Walter Roberson 2018년 4월 15일
ch_String = sprintf('%10.4f', db_Value);
No loop. This does everything at the same time.

Jonas Widmer
Jonas Widmer 2018년 4월 15일
Cool! Thanks a lot for the quick answer. That's an awesome solution. Didn't know the sprinf command. My actual application is however a little more complex than I explained above:
I have a matrix 3 x >100'000 and in the end I try to have this in a .txt file. Is there a way to assemble the matrix that I have this shape in the end:
' 5.2342 1.2315 156.1561\r\n 453.1236 12.1231 15.1231\r\n 1.1561 8.8912 5.5613\r\n'
When my matrix is:
my_Matrix = [5.2342, 1.2315, 156.1561;...
453.1236, 12.1231, 15.1231;...
1.1561, 8.8912, 5.5613]
Cheers, J

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by