I am executing a great-script which has inside it another mini-scripts.
Those scripts execute mathematical operations in order to calculate and show results of differente variables.
The issue is that Matlab do show those results correctly but some of the in this strange format:
How can I solve this problem and achieve Matlab giving me the results in a normal format? I think that this doesn`t happen if I execute those mini-scripts separately and individualy. It only happens when I execute the great-script. I also have tried writing
format short
In the head of each mini-script but seems that this didn't fix the issue.
Thanks!

댓글 수: 1

Stephen23
Stephen23 2021년 2월 6일
The variables EVfiT and EVfiT1 are symbolic or character or possibly some custom class... they are certainly not numeric, so the format command is totally irrelevant.

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

 채택된 답변

John D'Errico
John D'Errico 2021년 2월 5일

1 개 추천

These are symbolic expressions you are showing. The format command has NOTHING to do with symbolic expressions. It applies only to numeric values, thus typically double precision numbers.
X = 1 + sqrt(sym('13445664/2424536343636'))
X = 
X
X = 
format short
X
X = 
format rat
X
X = 
As you see, anything I do with format does not touch how we see X.
I cannot replicate what you did, because you show only a picture of your output. (If you really want help, then paste in the actual code, as text.)
But now, if I convert X to a double,
format short
Xd = double(X)
Xd = 1.0024
format rat
Xd
Xd =
1277/1274
format long g
Xd
Xd =
1.00235492336053
Now you see that format works, and it works as designed. I can turn it on and off on a whim. It applies to floating point numbers. The numbers that you show in that picture LOOK like numbers. But format does not understand symbolic expressions. It ignores them.

댓글 수: 9

I didn't use the command
sym('34/17')
In any moment
The point is that If I write my script like this (with a calling to a mini-script called scriptA which uses format rational) :
i=4/5
dv=186
m=10
%%%%%%%% scriptA %%%%%%%%
%Here it is the call to scriptA, but here I simplify like this so you can understand better and quicker
%so basically, this script does the following calculations:
format rational
d0=180
R1=80
R2=100
z1=16
z2=20
format short
%%%%%%%% scriptA %%%%%%%%
format short
Rf=(m*z2/2)-m
ab=R1/z1
The result Rf, ab and all the results that I would like to calculate after the call to scriptA will be given as a quotient. (in format rational)
And I don't know what does it has to do with symbolic expressions, because if I write this code:
i=4/5
dv=186
m=10
d0=180
R1=80
R2=100
z1=16
z2=20
Rf=(m*z2/2)-m
ab=R1/z1
All the results will be given perfectly, as default, as a number with decimals
Here is the thing and my doubt. Why once you write format rational, matlab is unable of going back to its default form (format short)?
Walter Roberson
Walter Roberson 2021년 2월 6일
See https://www.mathworks.com/help/matlab/ref/format.html#btiwmh5-2_1 for information on how to query the current format. You would ask for the current format at the point you have "format rational" and at the end of that section, reset to the old format. You might even want to use an onCleanup()
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras 2021년 2월 6일
Thanks for your replies!
I already read that and it doesn't solve my problem. Moreover, after using the format rational that I have mentioned before, when I look the settings, it appears that my 'Activevalue' is short... but it doesn't have much sense because the results keep appearing me as quotients
After the calculation, please ask
class(ab)
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras 2021년 2월 6일
Thanks for your reply
I did wrote class() in all calculated variables and some of them are 'double' and others are 'sym'
What does this mean?
John D'Errico
John D'Errico 2021년 2월 6일
편집: John D'Errico 2021년 2월 6일
You really need to go back to the basics.
syms u
u
u = 
u
format short
x = (1 + sqrt(5))/2
x = 1.6180
y = sym(x)
y = 
Here I have just created (and displayed) THREE variables. u, x, and y. One of them (u) is obviously a symbolic unknown. The other two are numbers. x and y are both representations of the golden number phi, but one is in symbolic form. However, once I have represented x as a double precision number, sym is not able to recover the actual form that had a radical in it. So sym just shows y as a fraction, the closest rational approximation that represents the number held in x.
We can force MATLAB to convert y into a floating point number, though still in symbolic form.
z = vpa(y)
z = 
1.6180339887498949025257388711907
Convertign back into a numeric form that format can use, we might have:
w = double(z)
w = 1.6180
t = int8(z)
t = int8 2
But now, what does MATLAB think those variables are, and how they are represented? whos will tell us. That last column is the result of what class will tell you for each variable.
whos u x y z w t
Name Size Bytes Class Attributes t 1x1 1 int8 u 1x1 8 sym w 1x1 8 double x 1x1 8 double y 1x1 8 sym z 1x1 8 sym
Only x and w are double precision numbers, though t is an integer variable. The other three are COMPLETELY unaffected by the display format you have chosen.
Any future computations I do with the variables u, y, and z will leave them still in symbolic form, unless I choose to convert them back into actual numbers.
format short
x
x = 1.6180
format long g
x
x =
1.61803398874989
format rat
x
x =
1597/987
And I can freely change between format styles. You are never "stuck" in any one format. I would also point out that format rat did NOT find the same rational approximation as was held in y, but this is just a display format.
So while you claimed in one comment that this did not solve your problem, the root cause of your problem is you do not yet understand MATLAB, and that you do not understand what you have done with the various variables you have created. The problem is NOT with the format command.
When you create a variable in symvbolic form, it will STAY in symbolic form. Anything you do with it will leave it as such, unles you convert it into a number, generally using a utility like double or single, etc. Format blithely leaves those numbers alone. Format looks only at numbers held in a true numeric format.
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras 2021년 2월 6일
Thanks a lot for your reply
But I still don't understand. In the example you give, you deliberately put y as a symbolic expression. But in my code, I don't put anywhere any variable as a symbolic expression (deliberately and in any way I think) , so I don't understand why matlab shows me some variables as a quotient and some of them no, and why some of them are symbolic expressions and why some other no
Walter Roberson
Walter Roberson 2021년 2월 7일
We do not have your scripts, so we would only be able to make wild guesses.
Wild guess: You thought you were doing numeric integration, but you used int(), which is reserved for symbolic integration.
Thanks a lot for your replies and your help, I just have found where was the mistake.
The point is that I had in the first mini-script this command
[numer,deno]=numden(sym(i));
Which calculated numer and deno as symbolic expressions and thus, the rest of the variables calculated along the code, which contains in their expressions numer or deno, are also symbolic expressions and that's why in my results appeared the results as quotients and not as decimal numbers.
I have fixed the issue by converting numer and deno to double expressions just like mate darova said in his comment
numerr=double(numer);
denoo=double(deno);

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

추가 답변 (1개)

darova
darova 2021년 1월 30일

0 개 추천

try double
a = 2;
b = 1/sym(a);
c = b/a
double(c)

댓글 수: 3

Steven Lord
Steven Lord 2021년 1월 31일
If double doesn't work because the symbolic expression contains a symbolic variable use vpa.
Thanks for your replies!
But I have found the issue. The point is that, as I said in the previous message, the great script is composed of several mini-script. The point is that at the start of one of those mini-script I have written
format rational
to calculate a specific thing in that mini-script. And then, Matlab continous using that format along the rest of the calculations of the script and THAT is the reason why Matlab shows those results as an mathematical expression, as I issued in the first message. So now the question is, how I "quit" the format rational command once used? How can I restablish the previous format?
As long as I know, the previous format, I mean, the standard format is
format short
And that is what I write as long as I finish using format rational in that mini script so matlab can work along the grat script normally and show the results normally, but apparently it doesn't work. What am I doing wrong?
"format" is a command, not a local setting for functions or subroutines.
If you had a
disp('hello')
command inside a function, would you expect the output of the function to disappear from the command window when the function returned? NO -- you would recognize that disp() changes state permanently, rather than disp() being a local setting within the function. Just so, "format" changes are permanent until changed.

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

제품

릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by