save syms into a file txt

17 views (last 30 days)
trung trinh
trung trinh on 23 Feb 2012
Commented: Walter Roberson on 28 Aug 2020
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

Walter Roberson
Walter Roberson on 23 Feb 2012
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
Hao Tan
Hao Tan on 28 Aug 2020
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 :)
Walter Roberson
Walter Roberson on 28 Aug 2020
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!