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.
Antonio
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)]

 채택된 답변

Abhishek Gupta
Abhishek Gupta 2011년 10월 5일

1 개 추천

One may use DIARY( http://www.mathworks.com/help/releases/R2011b/techdoc/ref/diary.html ) 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');
fclose(fid);
Hope this helps.
Abhi...

댓글 수: 2

mortain Antonio
mortain Antonio 2011년 10월 7일
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.
Ciao
Nachum Lerner
Nachum Lerner 2014년 1월 22일
Thanks for the great answer, helped also me.
Nachum

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 10월 5일

0 개 추천

results = char(solve('a*x^2 + b*x + c'));

댓글 수: 3

mortain Antonio
mortain Antonio 2011년 10월 5일
Hello Walter,
thanks for your answer, but this is not what I am looking for.
I want to save
%start code
>> syms a b c x;
>> results=solve('a*x^2 + b*x + c')
results =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
save results.dat results
%end code
I want to save in a text file:
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
I don't know if it is possible.
Using your approach, it saves some numbers:
1.0900000e+002 9.7000000e+001 1.1600000e+002 1.1400000e+002 1.0500000e+002 1.2000000e+002 4.0000000e+001 9.1000000e+001 9.1000000e+001 4.5000000e+001 4.0000000e+001 9.8000000e+001 3.2000000e+001 4.3000000e+001 3.2000000e+001 4.0000000e+001 9.8000000e+001 9.4000000e+001 5.0000000e+001 3.2000000e+001 4.5000000e+001 3.2000000e+001 5.2000000e+001 4.2000000e+001 9.7000000e+001 4.2000000e+001 9.9000000e+001 4.1000000e+001 9.4000000e+001 4.0000000e+001 4.9000000e+001 4.7000000e+001 5.0000000e+001 4.1000000e+001 4.1000000e+001 4.7000000e+001 4.0000000e+001 5.0000000e+001 4.2000000e+001 9.7000000e+001 4.1000000e+001 9.3000000e+001 4.4000000e+001 3.2000000e+001 9.1000000e+001 4.5000000e+001 4.0000000e+001 9.8000000e+001 3.2000000e+001 4.5000000e+001 3.2000000e+001 4.0000000e+001 9.8000000e+001 9.4000000e+001 5.0000000e+001 3.2000000e+001 4.5000000e+001 3.2000000e+001 5.2000000e+001 4.2000000e+001 9.7000000e+001 4.2000000e+001 9.9000000e+001 4.1000000e+001 9.4000000e+001 4.0000000e+001 4.9000000e+001 4.7000000e+001 5.0000000e+001 4.1000000e+001 4.1000000e+001 4.7000000e+001 4.0000000e+001 5.0000000e+001 4.2000000e+001 9.7000000e+001 4.1000000e+001 9.3000000e+001 9.3000000e+001 4.1000000e+001
Thanks again for your reply
Antonio
Walter Roberson
Walter Roberson 2011년 10월 6일
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 2011년 10월 7일
I will try in a different way and see what it happens....
Thanks for your reply, Antonio

댓글을 달려면 로그인하십시오.

Shahram Alipourazadi
Shahram Alipourazadi 2011년 10월 22일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2011년 10월 5일

댓글:

2014년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by