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!
0 个评论
回答(2 个)
Star Strider
2023-2-18
Try something like this —
syms t x(t) y(t)
symFunc = y == sin(x) + cos(x)
filename = 'Test.txt';
save(filename, 'symFunc','-mat')
file = which('Test.txt')
type(file) % Binary File
v = load(file,'-mat')
symFunc = v.symFunc
.
0 个评论
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!