a have a function like that
>> syms t
>> f = sin(t)
i want to save f into file .txt. Example name of file is a.txt with it's content: sin(t)
how to do that. Help and guide me.
Tk a lot.

 Accepted Answer

fid = fopen('a.txt', 'wt');
fprintf(fid, '%s\n', char(f));
fclose(fid);
Note: char() of a symbolic expression does not generally result in a string that can be evaluated by MATLAB. char() of a symbolic expression is really only suitable for entering the expression back in to the symbolic toolbox.

4 Comments

HOW TO SEPARATE EACH ROWS?
fid = fopen('a.txt', 'wt');
fprintf(fid, '[\n');
arrayfun(@(ROWIDX) fprintf('%s,',f(ROWIDX,1:end-1)) + fprintf('%s;\n', f(ROWIDX,end)), (1:size(f,1)).');
fprintf(']\n');
fclose(fid);
Answer looks good! Thank you for the input. It was very helpful.
One thing to note, I believe there should be 'fid' added into the 3rd line and 4th line right after fprintf so that the text file is opened and the entries are entered correctly:
fid = fopen('a.txt', 'wt');
fprintf(fid, '[\n');
arrayfun(@(ROWIDX) fprintf(fid, '%s,',f(ROWIDX,1:end-1)) + fprintf(fid, '%s;\n', f(ROWIDX,end)), (1:size(f,1)).');
fprintf(fid, ']\n');
fclose(fid);
Thanks again for the code :)
Good point, Hao Tan

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!