Output result's format?

40 次查看(过去 30 天)
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
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 个评论
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.
ErikJon Pérez Mardaras
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
try double
a = 2;
b = 1/sym(a);
c = b/a
double(c)
  3 个评论
ErikJon Pérez Mardaras
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?
Walter Roberson
Walter Roberson 2021-2-6
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by