How to save a symbolic function into a .txt file

44 次查看(过去 30 天)
Hello everyone,
I am trying to save a long symbolic function that I obtained as a result of my code into a .txt file. However, when I try to display it on the command window, it says "... Output truncated. Text exceeds maximum line length for Command Window display". As the solving procedure to obtain this function is quite time consuming, I would really like to save the result so that I can use it every time I want without having to go through the process again. Is there a way I can save a symbolic function (or a variable in general) permanently, for example in a .txt file?
I tried to use the command
save fileName.txt symFunc
but the resultant .txt file is completely unreadable.
I am using MATLAB R2021b. Any help is greatly appreciated. Thank you!

回答(2 个)

Star Strider
Star Strider 2023-2-18
Try something like this —
syms t x(t) y(t)
symFunc = y == sin(x) + cos(x)
symFunc(t) = 
filename = 'Test.txt';
save(filename, 'symFunc','-mat')
file = which('Test.txt')
file = '/users/mss.system.FSiTtZ/Test.txt'
type(file) % Binary File
MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Sat Feb 18 14:04:02 2023 ÚIMRxãc``èb6 æb`bv .®Ìu+ÍKòY|ý¡jâi¥y|@¶~^¨~V( j#TÄ0ï2!É08h xåVMÏ¢VF_çMg23iw]6³jC@>ÔI("näo\uÙeFÿHþnºïOèìª#öeÌ;Ó&]´IOrrïsîó{/÷÷%?>ûSûÉÉ{RáÆÉgölÍ 1wÆ/OþËÓ}óýÕ/ñÖ»±FÅáú¼xÍóC-Ïg¿ñgÛzÇ=ã':;CÔ04¢o¿­ôBMÿ0ß?¿YÇ_4>¼o¦ñ>¿YÅZÀE#ü»Ê>9ܸ_-S£_MlǧI&ÀÀ±ä-ý³i<nOnðýxukÞàFk<«ÇZÀÃ>ïjã­ _ÏéÊ»®åî&×]-Ï{»¦º½¬µ¿×ΡUÅëõóSãaO+|zû­ûúµí}1Àúò1)%ðùëúa¢'ðp¾ãsÒc×1'¦iìk±=§\/ö°­ÐlÇ<Äea$Ç[&¼±NvÛGÔ^½±D@.r¡3ZCô:[%æTå:¼ hr&¬¦. p5Á¨U;[HÞÈÞý%2ßt°Â2=&C°¸C3[î,¸q Ë¥CÄ-4ÑMo¾°!´ÌKÊp®×wY°Ùh¦Ì-nÂûép¹ïËVrÐgª`æ ¨9¼ÆV8AC÷pÜ®ÛP&ª|ITØæ Ñaè9åù(SC »Ø*£3s»½®¥8O.'µÞ$ÁA£$» ,Ôò6)gèú4-µÙÖù3DU;08ÅÙ¶#3ä(°JP¶t·L¨z¡ÙñAóMr¿úúöWÕùÃÀÃùßß½Ïv}OϺîGt·ß§ïå{óæ¬gjú7úûý~éþÚ¬]ûïöÑüx¿½©ã·§7)l²!;Óse)÷i´ß·'·yp6ÏÒ*O~àQí£ñM'Å ±8ïÆ&êg[&á4p¤·®D»Ej`ïÎu\Ѹ$É)>¶È±u¹Ó-@²¿(´QMÚé.i-0k°cÏ]J!ãçrPLû«Çí4½mÆÎËiÆ)H/Dð·Ü~ ¢«ûué9åVþlo±d°PƵ4É25v+Wm«6ÂUý, N+i(»G¦2Èi"§;£î?úQY¥`ºFA¸ÑÚØË;´X['õx³mÊ£ 58'Rw3-óÔkóNvVp6³£ÆôÔJíWyQ]³î)'«Î3é¥;تÙ@Öôã::¸Û´Q¼îvÐ\2TLV{G'ÅNl{k+¶Ä P1¬ý7×Ú¿Öñ¿Uÿ¿þ_ÞÝèoç{ì?¼§õÓ¿©éÿÅ~Ô8uû?>·ïß¿Yáúý°þý'÷óý sø
v = load(file,'-mat')
v = struct with fields:
symFunc: [1×1 symfun]
symFunc = v.symFunc
symFunc(t) = 
.

John D'Errico
John D'Errico 2023-2-18
编辑:John D'Errico 2023-2-18
You don't need to display it in the command window! For example, this expression will have 10001 terms in it.
syms x
P = expand((x+2)^10000);
Now you can save the variable P into a .mat file, and then load that file in again. There is no reason to worry about the command window truncating the result.
Or, if you want it in a more readable form, then convert it to a chanracter form.
Cp = char(P);
Again, use a semi-colon on the line so you don't trash the command window. Now save that into a text file if you prefer. A .mat file is by far preferable of course.

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by