Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a matrix (input) and want to export it as a text file (output), therefore, I am using the following code in matlab:

save('out.txt', 'input', '-ASCII');

My question is that how I can insert for example 3 lines (as follow) for its header? I don't want to open the output.txt file in another program, because the size of the output.txt is very large and non of the available softwar can open it. Therefore, I want to do this directly in matlab.

These data set are...
It is created by
2013
share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

I think you cannot do it using only save function. For a quick, I can see two options that might be useful.

First. Create a file with the header and then use save with -append options:

input = rand(5);
header = ['These data set are It is created by 2013'];

fileID = fopen('out.txt','w');
fprintf(fileID,'%s\n', header);
fclose(fileID);

save('out.txt', 'input', '-ASCII', '-append'); 

Second. Instead of using save, manually use fprintf to write everything:

input = rand(5);
header = ['These data set are It is created by 2013'];

fileID = fopen('out.txt','w');
fprintf(fileID,'%s\n', header);
fprintf(fileID,[repmat('%f ', [1, size(input, 2)]),'\n'], input);
fclose(fileID);

If u want multi-line header, u can do as follows:

header = ['These data set are ...\nIt is created by\n2013'];


fileID = fopen('out.txt','w');
fprintf(fileID, [header, '\n']);
fprintf(fileID,[repmat('%f ', [1, size(input, 2)]),'\n'], input);
fclose(fileID);
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.