save syms into a file txt

22 Ansichten (letzte 30 Tage)
trung trinh
trung trinh am 23 Feb. 2012
Kommentiert: Walter Roberson am 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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
Hao Tan
Hao Tan am 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 am 28 Aug. 2020
Good point, Hao Tan

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by