How do I print a mixed expression ( numeric and symbolic) to a file?

조회 수: 2 (최근 30일)
Jonathan Dentch
Jonathan Dentch 2014년 3월 27일
답변: Star Strider 2014년 3월 28일
Example: 3.21 - x
  댓글 수: 2
Star Strider
Star Strider 2014년 3월 28일
How do you generate your expressions? Symbolic Math Toolbox? Something else?
Jonathan Dentch
Jonathan Dentch 2014년 3월 28일
A = [1,2;3,4];
// Greek letter xi
xi = sym('xi');
B = [xi,0;0,xi];
C = A - B;
fprintf(fid,C(:,:));

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

답변 (3개)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014년 3월 27일
You can use sprintf or easier way is to concatnate strins
myString=sprintf();
fprintf(myFile,myString);
or
fprintf(myFile,[num2str(myNumber),' - x']);
and then write the 'mySring' to a file.
I personally never used sprintf , so for furter details abou sprintf refer to MALAB help
doc sprintf
Good Luck!
  댓글 수: 1
Jonathan Dentch
Jonathan Dentch 2014년 3월 27일
How do I print a mixed expression in general ? The expression might contain several numerics and several symbolics in any order.

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


Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014년 3월 28일
The only way that I know of is to concatnate just as I showed u! I edited a whole bunch of text files like this.
I don't think if there would be another way around. Just to make it more automatic you may put your strins in a cell array, and also the digits, and use a for loop to do it easier!
names={'Alex','Sam','Max','Sala','Tom','Tim'};
examResult=(20 10 4 13 5 0);
for i= 1:lenght(examResult)
fprintf(myFile,[names(i),'''s exam result=',num2str(i)]);
end
output
Alex's exam result= 20
Sory if it is difficult, sometime I'm looking for super easy ways that does not exits. I don't think if an specific way would exis for you.
  댓글 수: 1
Jonathan Dentch
Jonathan Dentch 2014년 3월 28일
Thanks for your help, Salaheddin. However, I do not know the exact composition of the mixed expression (i.e., one numeric followed by an operator followed by one symbolic). Your example contains no symbolics. MATLAB is able to print a mixed expression to the command window. Surely, there must be a way to print the same expression to a file.
A = [1,2;3,4]; xi = sym('xi'); // Greek letter xi B = [xi,0;0,xi]; C = A - B; fprintf(fid,C(:,:));

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


Star Strider
Star Strider 2014년 3월 28일
The only MATLAB functions I can find that deal with unicode characters are unicode2native (and native2unicode that may not apply to what you are doing but has a link at the end of the unicode2native page).
Consider this for a start, and experiment:
bytes = unicode2native('Ξ ξ')

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by