0

I have a cell array of strings called myarray, containing n elements. I want to write the array to a text file called myfile.txt. I want every line in the text file to correspond to one element in the cell.

When I try the following:

fid = fopen('myfile.txt', 'w');
for i=1:n
    fprintf(fid, '%s\n', myarray{i});
end

However, this outputs a file without any carriage returns after each element. When I open it in Notepad in Windows, I see just a list of characters which is from the strings in myarray concatenated together. However, I want each string to be on its own line.

What's going on?

2 Answers 2

2

With NotePad you might want to use '\r\n' instead of '\n'. Hence:

fid = fopen('myfile.txt', 'w');
for i=1:n
    fprintf(fid, '%s\r\n', myarray{i});
end

I can't test it right now though. Take a look at this:

Taken from the Matlab help:

Some Windows® text editors, including Microsoft® Notepad, require a newline character sequence of '\r\n' instead of '\n'. However, '\n' is sufficient for Microsoft Word or WordPad

http://www.mathworks.com/help/matlab/import_export/write-to-delimited-data-files.html#br2ypq2-1

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

Comments

0

You have two options.

Comments

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.