How to save a symbolic function into a .txt file
조회 수: 39 (최근 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!