0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. In fact, I tried using character vector as a filename as the error states but it seems it doesn't fix the issue. I have modified my post showing the error message.
Try checking what's in L. whos('L') would also help you get more info on it. I've never had to use csvwrite in an inconsistent cell or object array, so I'd be clueless with those cases. 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, it would also be a good time for you to improve on your debugging skills.
Fun Fact: Now this was the first result google showed me. I thought answers shouldn't just link to other resources. Also we have the chance to document brief answers here, so people don't have to wade through docs.
@qacwnfqq The original post before it was edited had OP show a different error. To quote: "I did Google it to find the issue but nothing found. Kindly, what's wrong with this?". My answer was meant as a joke on OP's effort to search for the error, but I'll edit my answer the original question to sound more formal since the context is lost now. I will not be answering the edited one since it's more of an issue with OP's sample data rather than the function csvwrite itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.