MATLAB: Sprintf: how to efficiently create a string of 75 numbers, separated by comma

sprintf

I have an array which contains 75 numeric elements. I want to write these numbers as a string separated by a comma. If there were few numbers, I could use 'sprintf' as follows:
A=[1,2];
str=sprintf('%d,%d',A(1:end));
I need an efficient way to do it when there are many entries in A.

Best Answer

You can still use sprintf. You just need to create a ‘dynamic’ format string:
A = 1:5;
str = sprintf([repmat('%d,',1, numel(A)-1), '%d'], A)
str =
'1,2,3,4,5'
That will adapt for any ‘A’ vector.