Reputation: 61
I have a vector with 4225 elements that its elements are separated by spaces and I have to use this vector in MuPAD as edge weight matrix of a directed graph. In order to make this vector accessible in MuPAD as a graph edge weight matrix, its elements should be separated by commas. Since the number of elements is huge, it's a waste of time to write commas between them one by one. So is there any simple way to do this in matlab?
Big thanks in advance
Upvotes: 1
Views: 5199
Reputation: 38032
This ought to do the trick:
%// example vector
a = [4 5 6 7 8 9 10 11 12 13];
%// replace all consecutive spaces with a comma
aCSV = regexprep(num2str(a,17), '\s*', ',')
Output:
aCSV =
4,5,6,7,8,9,10,11,12,13
Upvotes: 3
Reputation: 18484
Here's a version using only sprintf
:
v = [1 2 exp(1) 3 pi 4 5 realmax];
s = sprintf('%.17g,',v); % Up to 17 decimal places (double precision has about 16)
s = s(1:end-1); % Remove trailing comma
This returns
s =
1,2,2.7182818284590455,3,3.1415926535897931,4,5,1.7976931348623157e+308
See this article for details on using format strings with sprintf
if you wish to further customize this.
Upvotes: 3