2

I have a cell array that has both characters and numbers as values. What is the simplest way to output it in a csv file, row by row, maintaining the structure of the cell array? For example, if the cell array is

[abc] [1] [131]
[def] [] []
[gh] [13] [999]

I want the file to look like

abc,1,131
def,,
gh,13,999
1

1 Answer 1

5

Example:

%# cellarray
C = {
    'abc' [1]  [131]
    'def' []   []
    'gh'  [13] [999]
};

%# write line-by-line
fid = fopen('file.csv','wt');
for i=1:size(C,1)
    fprintf(fid, '%s,%d,%d\n', C{i,:});
end
fclose(fid);
Sign up to request clarification or add additional context in comments.

2 Comments

Should this file already exist or I create it this way? I am getting ??? Error using ==> fprintf Invalid file identifier. Use fopen to generate a valid file identifier. Error in ==> parseTickData at 134 fprintf(fd, '%s,%f,%d%f%d%f%d\n', C{i,:});
@Trup: it should create a new file if it didn't already exist, and overwrite it if it did.. Also can you show us what a row in your actual cellarray looks like?

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.