Kristofer
Kristofer

Reputation: 1487

Issue when writing matrix to file using csvwrite

I am trying to output the matrix to a CSV file (comma separated) using this function csvwrite('myMatrix.dat',L); ( where L is square matrix) I got this error:

>> csvwrite('myMatrix.dat',L);
Error using sprintf
Function is not defined for sparse inputs.

Error in dlmwrite (line 169)
        str = sprintf(format,m(i,:));

Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);

Kindly, what's wrong with this?

Upvotes: 0

Views: 1170

Answers (1)

ToninGuy3n
ToninGuy3n

Reputation: 328

This answer was to answer OP's original error from using:

csvwrite(string('myMatrix'),L);

Error using csvwrite (line 30) FILENAME must be a character vector. The error you're seeing is an issue with your input (arguments). It's telling you that the file name must be a "character vector".

According to Matlab's documentation:

There are two ways to represent text in MATLAB®. Starting in R2016b, you can store text in string arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for character arrays is to store pieces of text as character vectors. MATLAB displays strings with double quotes and character vectors with single quotes.

http://mathworks.com/help/matlab/matlab_prog/creating-character-arrays.html#briuv_1-1

In simple words. The wrong type was being used as an argument.

To provide a hint for debugging the new error.

Limitations csvwrite writes a maximum of five significant digits. If you need greater precision, use dlmwrite with a precision argument.

csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite.

http://mathworks.com/help/matlab/ref/csvwrite.html?requestedDomain=www.mathworks.com

Try checking what's in L. whos('L') would also help you get more info on it. An easy way to view what's in your variables, is double click from the workspace. Another way is by creating a break point on your call to csvwrite within a script, then use the debugger and calling for L once you know it's loaded into memory. If you still don't know what's going on, then try 'step in' line by line.

csvwrite does not accept cell arrays.

Upvotes: 1

Related Questions