Save Sym data per variable

7 views (last 30 days)
mortain Antonio
mortain Antonio on 5 Oct 2011
Commented: Nachum Lerner on 22 Jan 2014
Hello, I have a code which deals with symbolic variables and I have as output a vector whose dimension is known but variable.
The output is symbolic, for example a=[y_1/3+y_2/5 y_1/10+y_2/37]
I would like to save in an ASCII file, not binary, the output and for each row have one element of the vector. The tricky part is the saving in a file, I can do it only in bin, since when I type -ASCII it doesn't do it.
Here I attach a piece of code useful to understand. Thanks for your time.
syms a b c x;
results=solve('a*x^2 + b*x + c');
save results.dat results
My output comes as:
results=[-(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a)]

Accepted Answer

Abhishek Gupta
Abhishek Gupta on 5 Oct 2011
One may use DIARY( ) to capture any output to the command window into a text file.
Otherwise, you may convert your symbolic variables into strings and write them into a text file, for instance:
syms a b c x;
results=solve('a*x^2 + b*x + c');
fid = fopen('myfile.txt', 'w');
fwrite(fid, char(results), 'char');
Hope this helps.
mortain Antonio
mortain Antonio on 7 Oct 2011
Dear Abhi, thanks a lot for your reply.
I wanted to isolate the single equations and the coefficients....I guess I need to work a bit on it, but what you wrote is useful.
Nachum Lerner
Nachum Lerner on 22 Jan 2014
Thanks for the great answer, helped also me.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 5 Oct 2011
results = char(solve('a*x^2 + b*x + c'));
Walter Roberson
Walter Roberson on 6 Oct 2011
This is not something I can test myself, but I can tell you that that output does not look as expected and documented.
mortain Antonio
mortain Antonio on 7 Oct 2011
I will try in a different way and see what it happens....
Thanks for your reply, Antonio

Sign in to comment.

Shahram Alipourazadi
Shahram Alipourazadi on 22 Oct 2011
To isolate each row you can write:
fid = fopen('myfile.txt', 'w');
for i=1:length(results)
fprintf(fid, '%s\n',char(results(i)));
Generally first you should convert each argument into string and then using string manipulation functions arrange them

Community Treasure Hunt

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

Start Hunting!