From: Michael on
I would like to write a matrix to a text file from matlab. In addition, I want to include both column and row headers to avoid confusion. Is there anyway to do this without using fprintf to write each value separately? Ideally, one could use fprintf to enter in the headers and dlmwrite or something similar to write the numerical values but the -append parameter in dlmwrite automatically inserts a new line.

Thanks,
Mike
From: dpb on
Michael wrote:
> I would like to write a matrix to a text file from matlab. In addition,
> I want to include both column and row headers to avoid confusion. Is
> there anyway to do this without using fprintf to write each value
> separately? Ideally, one could use fprintf to enter in the headers and
> dlmwrite or something similar to write the numerical values but the
> -append parameter in dlmwrite automatically inserts a new line.
> Thanks,
> Mike

Not tried anything specifically; suppose you could switch the formatting
to creating a cell array and output it, perhaps.

Personally I'd just encapsulate the desired functionality in a function
and call it. w/ the vectorized fprintf() it would only be header line
followed by a loop on the rows in the crude/straightforward way.

If the strings for the row headers are in string array "s" and the data
in "x", the following is the guts for the output this way...

for idx=1:length(x,1))
fprintf(['%s ' repmat('%4.2f ',1,size(x,2)) '\n'],s(i,:),x(i,:));
end

Salt to suit...

--
From: TideMan on
On Jan 20, 8:12 am, "Michael " <michael.gorm...(a)gmail.com> wrote:
> I would like to write a matrix to a text file from matlab.  In addition, I want to include both column and row headers to avoid confusion.  Is there anyway to do this without using fprintf to write each value separately?  Ideally, one could use fprintf to enter in the headers and dlmwrite or something similar to write the numerical values but the -append parameter in dlmwrite automatically inserts a new line.  
>
> Thanks,
> Mike  

What makes you think that fprintf writes each value separately?
fmt=[repmat('%f,',1,size(A,2)) '\n'];
fprintf(fid,fmt,A');
would print out the whole matrix A in comma delimited rows.
Note the transpose of A because Matlab stores data column by column.