Effacer les filtres
Effacer les filtres

How to display workspace results with certain digits

2 vues (au cours des 30 derniers jours)
Fotis
Fotis le 21 Mai 2015
Commenté : Thorsten le 22 Mai 2015
Hi all.
I have a program that calculates temperatures and pressures. My results look like this: T=298.3456 etc and P=2.38947 I have written a program in order to display these results in a graph but I don't want them to show up with so many digits! Anyone knows how to do this? For example, make them show up like T=298 and P=2.40??

Réponses (2)

Jan
Jan le 21 Mai 2015
Simply round the values to n digits:
round(x * 10^n) / 10^n
There are many other tools for rounding to significant digits e.g. in the FileExchange. Use the methods to search there or in the internet.
  1 commentaire
John D'Errico
John D'Errico le 21 Mai 2015
round now allows you to round to n digits.

Connectez-vous pour commenter.


Thorsten
Thorsten le 21 Mai 2015
num2str(P, '%.2f')
num2str(T, '%.0f')
  2 commentaires
Fotis
Fotis le 21 Mai 2015
That's it! Thanks! Do you perhaps also know how to apply this to a multifield structure??
I have for e.g T.w.hp , T.w.lp, T.eg.in etc etc. Your answer applies perfectly to each one of them but is it a way to do it for all T's at once? My program is huge and I have a lot of substructures. Thanks anyway
Thorsten
Thorsten le 22 Mai 2015
I'm not sure if that's what you are looking for, but you can use the following syntax
num2str([T.w.hp T.w.lp T.eg.in], '%.2f ')
If you structure T contains only numerical data that you like to be printed in the same format, you can use
num2str(flatten(y), '%.2f ')
with my flatten function
function [y, me] = flatten(x)
%FLATTEN Flatten numeric data (ND matrices or arbitrarily nested cells)
%
% [Y, ME] = FLATTEN(X)
%
%Sample usage:
% C = {1; {2,3}; {4,5}; {6,{7,8}}}
% flatten(C)
%
% S.pos.x = 10; S.pos.y = 12; S.id = 23; S.pos.q.offset = 0.1;
% flatten(S)
%
% Thorsten.Hansen@psychol.uni-giessen.de 2015-05-21
if iscell(x)
y = [];
for i = 1:numel(x)
try
xi = cell2mat(x{i});
catch me
xi = flatten(x{i});
end
y(end+1:end+numel(xi)) = xi;
end
elseif isstruct(x)
y = flatten(struct2cell(x));
else
y = x(:);
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by