Q:

Save a sparse array in csv

I have a huge sparse matrix a and I want to save it in a .csv. I can not call full(a) because I do not have enough ram memory. So, calling dlmwrite with full(a) argument is not possible. We must note that dlmwrite is not working with sparse formatted matrices.  
The .csv format is depicted below. Note that the first row and column with the characters should be included in the .csv file. The semicolon in the (0,0) position of the .csv file is necessary too.
;A;B;C;D;E
A;0;1.5;0;1;0
B;2;0;0;0;0
C;0;0;1;0;0
D;0;2.1;0;1;0
E;0;0;0;0;0

Could you please help me to tackle this problem and finally save the sparse matrix in the desired form?

A:

You could do this iteratively, as follows:
A = sprand(20,30000,.1);

delimiter = ';';
filename = 'filecontaininghugematrix.csv';

dims = size(A);
N = max(dims);

% create names first
idx = 1:26;
alphabet = dec2base(9+idx,36);
n = ceil(log(N)/log(26));
q = 26.^(1:n);
names = cell(sum(q),1);
p = 0;
for ii = 1:n
    temp = repmat({idx},ii,1);
    names(p+(1:q(ii))) = num2cell(alphabet(fliplr(combvec(temp{:})')),2);
    p = p + q(ii);
end
names(N+1:end) = [];

% formats for writing
headStr = repmat(['%s' delimiter],1,dims(2));
headStr = [delimiter headStr(1:end-1) '\n'];
lineStr = repmat(['%f' delimiter],1,dims(2));
lineStr = ['%s' delimiter lineStr(1:end-1) '\n'];

fid = fopen(filename,'w');

% write header
header = names(1:dims(2));
fprintf(fid,headStr,header{:});
% write matrix rows
for ii = 1:dims(1)
    row = full(A(ii,:));
    fprintf(fid, lineStr, names{ii}, row);
end

fclose(fid);

The names cell array is quite memory demanding for this example. I have no time to fix that now, so think about this part yourself if it is really a problem ;) Hint: just write the header element wise, first A;, then B; and so on. For the rows, you can create a function that maps the index ii to the desired character, in which case the complete first part is not necessary.

