How to limit the output decimal places?
Mostra commenti meno recenti
Hi everyone,
I required upto 10 decimal place answer for my output, but after few decimal place its shows zero. Earlier i attempted with longEng command but still the issue is same. May someone help put me here.
my script is as follows
clear all
clc
raw=readmatrix('Hourly_ev_05_04_2022');
data_a=raw';
data_a1=data_a(1:24,:);
rate=(mean(data_a1))';
data_a2=data_a(25:96,:);
ev_hr=data_a2;
bbb=rate;
position = 0; % position of plot in subplot
fig_num = 1; % figure() number
for S=184:184
uu=S;
r=bbb(uu); % for first event
data=ev_hr(:,uu);
u=transpose(data);
for n=1:length(u);
dt=n;
for i=1:length(u)-(n-1);
K=u(i:i+n-1);
k=sum(K);
b(i,n)=(k-(r)*(dt))/sqrt(r*dt);
end
end
T=transpose(b);
%format long
for ii=1:72
for jj=1:72-(ii-1)
if(ii==1)
b1(ii,jj)=T(ii,jj);
else
b1(ii,jj+(ii-1))=T(ii,jj);
end
end
end
ddd=max(b1);
vvv=max(ddd);
for ii=1:length(b)
for jj=1:72
if(b1(ii,jj)==0)
b1(ii,jj)=nan;
end
end
end
yyy=max(b1);
pp=max(yyy);
beta_new=pp';
c = NaN(72,24);
c2=[c b1];
x = [-23:72]' ;
y = [1:72]' ;
figure;
ax1=subplot(2,1,1);
pcolor(x,y,c2);
shading interp ;
cb = colorbar('Location','west')
cb.Label.String = '\beta';
cb.Label.Rotation = 360;
set(cb.Label,'FontSize',11);
ylh=ylabel('Time interval (hr)', 'FontSize', 9);
xline(0,'--r', 'LineWidth', 1.5);
xline(-0.4,'-k', 'LineWidth', 1.5);
set(gca,'XTickLabel',[]);
ax1_pos = get(ax1,'Position');
yl=ylim;
ax = gca;
ax.FontSize = 8;
grid on
patch([46 53 53 46], [yl(1) yl(1) yl(2) yl(2)], [0.01 0.01 0.01], 'FaceAlpha', 0.2 )
%patch([27 35 35 27], [yl(1) yl(1) yl(2) yl(2)], [0.01 0.01 0.01], 'FaceAlpha', 0.2 )
ylh.Position(1) = ylh.Position(1) - 0.2;
text(50,yl(2)-5,sprintf('\\beta_{max} = %.1f',beta_new), ...
'VerticalAlignment','top');
% text(30,yl(2)-20,sprintf('\\beta_{max} = %.1f',16.1), ...
% 'VerticalAlignment','top');
%middleCentered
ax2=subplot(2,1,2);
set(ax2, 'Position', [.13 .3 .775 .25]);
A=data_a(:,uu);
ss= sum(A)+20;
b=cumsum(A);
h=[-23:72];
plot(h,b, 'b', 'LineWidth', 1.5)
ylabel('Cummulative events', 'FontSize', 9)
xlabel('Time (hr)', 'FontSize', 9)
xline(0,'--r', 'LineWidth', 1.5);
xline(-0.4,'-k', 'LineWidth', 1.5);
axis([-23 72 0 ss])
yl=ylim;
% text(25,yl(2),sprintf('\\beta_{max} = %.1f',beta_new), ...
% 'VerticalAlignment','top');
grid on
patch([46 53 53 46], [yl(1) yl(1) yl(2) yl(2)], [0.01 0.01 0.01], 'FaceAlpha', 0.2 )
%patch([27 35 35 27], [yl(1) yl(1) yl(2) yl(2)], [0.01 0.01 0.01], 'FaceAlpha', 0.2 )
ax = gca;
ax.FontSize = 8;
%f = gcf;
%exportgraphics(gca,'Ev_01.png','Resolution',300)
end
Here is the output (3 and 5 coloumn and 12th rows showing zero).

Risposte (1)
Walter Roberson
il 8 Apr 2022
0 voti
The format command does not affect the Variable Browser.
The format for the variable browser can be changed for each variable. While you are browsing the variable, click on the View tab, and there is a drop-down there for Number Display Format.
You can change the default display format for the variable browser using Preferences -> Variables -> Format (Default array format)
10 Commenti
Andi
il 8 Apr 2022
Walter Roberson
il 8 Apr 2022
It would help if you could attach your data and all code relevant to that section for us to test.
When you use the variable browser (which I can see you are using from the structure of the output window), then the variable browser never displays a value as plain 0 unless the value is exactly zero, or exactly negative zero. If the value was non-zero but too small to display to the digits of precision you asked for, then it would display as 0.0000
So those values that you point out are exactly zero, and we need to figure out why.
Your code is a bit difficult to follow, as it does some unnecessary transpose operations.
Andi
il 8 Apr 2022
Walter Roberson
il 8 Apr 2022
When I run that code with that data, the lower right triangle of T is 0, but nothing else is. I do not get any holes in the output.
Andi
il 8 Apr 2022
No, it is not a matter of decimal places being displayed. Put some debugging code in and you can see that with that data file, the values calculated at the locations you note are definitely not zero, and MATLAB doesn't show them as zero.
In the below, the "saved" values are there for comparison to be sure that we were examining the proper locations in the debugging phase.
Look at the spy() results: the zeros are strictly below the diagonal of T.
format long
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/956880/Hourly_ev_05_04_2022.csv';
raw = readmatrix(filename);
data_a=raw';
data_a1=data_a(1:24,:);
rate=(mean(data_a1))';
data_a2=data_a(25:96,:);
ev_hr=data_a2;
bbb=rate;
position = 0; % position of plot in subplot
fig_num = 1; % figure() number
for S=184:184
uu=S;
r=bbb(uu); % for first event
data=ev_hr(:,uu);
u=transpose(data);
for n=1:length(u);
dt=n;
for i=1:length(u)-(n-1);
K=u(i:i+n-1);
k=sum(K);
b(i,n)=(k-(r)*(dt))/sqrt(r*dt);
if n == 12 & ismember(i, [3 5])
display(k); display(r); display(dt)
fprintf('k-(r)*(dt) = %.15g\n', (k-(r)*(dt)));
fprintf('b(i,n) = %.15g\n', b(i,n));
save_this(i) = b(i,n);
end
end
end
T=transpose(b);
fprintf('T(12,3) = %.15g\n', T(12,3));
fprintf('T(12,5) = %.15g\n', T(12,5));
fprintf('saved (12,3), (12,5) = %.15g, %.15g\n', save_this([3 5]));
end
spy(T==0)
Andi
il 8 Apr 2022
Walter Roberson
il 8 Apr 2022
Note that I did not change anything about the logic of the code: I just added display() and fprintf() and spy()
Andi
il 8 Apr 2022
Walter Roberson
il 10 Apr 2022
When you run the code with the debugging added, on your system, what output is shown by the debugging?
If you have a recent enough MATLAB, then you should be able to copy and run exactly the code I posted, without even modifying it to change the file name. If it works that way but fails when you change the file name to refer to your local system, then that would give us more information about what to look for.
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

