copyobj legend and facealpha issue when setting log yscale - matlab

I have the following code for drawing a picture:
%scamp data
[flipedBV, flipeddepth] = flipit(depthi, BVsurf_int_ens);
i=linspace(0,5,100);
edges_eps=10.^(-i);
logedg_BV= log10(fliplr(edges_eps));
[n_BV,xout_BV] = histc(histazza(log10(flipedBV)), logedg_BV);
x = logedg_BV;
%model data
[n_BVn,xout_BVn] = histc(histazza(log10(sqrt(-BVsurf_Num_ens))), logedg_BV);
BVsurfF = figure;hold on
h=area(x,n_BV/sum(n_BV),'facecolor',[1 0 0]); %%red area where the problem gonna be
legend('SCAMP')
xlabel('$$ N~[1/s]$$','Interpreter','latex','fontsize',18)
set(gca,'fontsize',14,'ygrid','on')
alpha(.5) %%translucency of the red area
%%add new data
addLineToFig('KEPflu', [0 0 1], BVsurfF, 'BVsurf_Num_ens', logedg_BV);
addLineToFig('KEPflu2', [0 1 1], BVsurfF, 'BVsurf_Num_ens', logedg_BV);
addLineToFig('GASflu', [0 0 0], BVsurfF, 'BVsurf_Num_ens', logedg_BV);
addLineToFig('GASflu2', [1 0 1 ], BVsurfF, 'BVsurf_Num_ens', logedg_BV);
addLineToFig('EPSmin', [1 1 0], BVsurfF, 'BVsurf_Num_ens', logedg_BV);
addLineToFig('GASmin', [.5 .5 0], BVsurfF, 'BVsurf_Num_ens', logedg_BV);
addLineToFig('canuto', [.5 .5 .5]', BVsurfF, 'BVsurf_Num_ens', logedg_BV);
the subroutine addLineToFig consists in:
function addLineToFig(name, ccol, fighandle, variab, x)%, flippo, depthi)
cd(['E:\SIMULATIONS\',name,'\COMPARED\ensamble']);
load([name,'_ensamble'], variab);
[n_BVn, xout_BVn] = histc(histazza(log10(sqrt(-BVsurf_Num_ens))), x); %%new data
figure(fighandle)
[LEGHbv,OBJHbv,OUTHbv,OUTMbv] = legend;
P=plot(x,n_BVn/sum(n_BVn),'color',ccol,'linewidth',2); %%plot new data
legend([OUTHbv;P],OUTMbv{:},name) %%update legend
end
Basically, I create a plot of the red area and then add data with addLineToFig and correctly obtain:
The problem arises when I try to duplicate the figure:
h1=gcf;
h2=figure;
objects=allchild(h1);
copyobj(get(h1,'children'),h2);
set(gca,'yscale','log')
As you can see the translucency of the red distribution is not duplicated and the legend has some problems.
The problem appears to be the last line where I set the yscale to log. If I comment it the code works fine. Does anyone know a workaround?
Minimal code
i=linspace(0,5,100);
edges_eps=10.^(-i);
logedg_BV= log10(fliplr(edges_eps));
a = 1e-5;
b = 1e-2;
r = (b-a).*rand(1000,1) + a;
[n_BV,xout_BV] = histc(histazza(log10(r)), logedg_BV);
x = logedg_BV;
%model data
r2 = (b-a).*rand(1000,1) + a;
[n_BVn,xout_BVn] = histc(histazza(log10(r2)), logedg_BV);
BVsurfF = figure;hold on
h=area(x,n_BV/sum(n_BV),'facecolor',[1 0 0]); %%red area where the problem gonna be
legend('SCAMP')
xlabel('$$ N~[1/s]$$','Interpreter','latex','fontsize',18)
set(gca,'fontsize',14,'ygrid','on')
alpha(.5) %%translucency of the red area
%%add new data
r3 = (b-a).*randn(1000,1) + a;
[n_BVn,xout_BVn] = histc(histazza(log10(r3)), logedg_BV);
figure(BVsurfF)
[LEGHbv,OBJHbv,OUTHbv,OUTMbv] = legend;
P=plot(x,n_BVn/sum(n_BVn),'color','k','linewidth',2);
legend([OUTHbv;P],OUTMbv{:},'data2')
%%add new data
r4 = (b-a).*rand(1000,1) + a;
[n_BVn,xout_BVn] = histc(histazza(log10(r4)), logedg_BV);
figure(BVsurfF)
[LEGHbv,OBJHbv,OUTHbv,OUTMbv] = legend;
P=plot(x,n_BVn/sum(n_BVn),'color','y','linewidth',2);
legend([OUTHbv;P],OUTMbv{:},'data3')
h1=gcf;
h2=figure;
objects=allchild(h1);
copyobj(get(h1,'children'),h2);

I solved the problem by setting the renderer of the original figure to 'OpenGL':
set(BVsurfF,'renderer','OpenGL')

Related

Edge Detection with Sobel Masks in Matlab this code is not accepting below mentioned line

Edge Detection with Sobel Masks in Matlab this code is not accepting below mentioned line
this code is giving error at res = maskx.*bwsquare;
i have watched step by step tutorial on the internet but still its not accepting this line
clear
clc
close all
origional = imread('q1jjy.png');
rubic = origional(:,1:500,:);
figure()
imshow(rubic)
bw= uint8((1/3)*(double(rubic(:,:,1))+double(rubic(:,:,2))+double(rubic(:,:,3))));
figure()
imshow(bw)
bwdbl = double(bw);
figure()
maskx = [-1 -2 -1, 0 0 0, 1 2 1 ];
[r,c] = size(bw);
OUT = zeros(r-3, c-3);
for idx = 1:(r-3)
for jdx = 1:(c-3)
bwsquare = bwdbl(idx:(idx+2), jdx:(jdx+2));
res = maskx.*bwsquare;
OUT(idx,jdx)=sum(sum(res));
end
end
Gx= OUT;
figure()
imshow(Gx)
masky = [-1 0 1; -2 0 2; -1 0 1];
for idx = 1:(r-3)
for jdx = 1:(c-3)
bwsquare = bwdbl(idx:(idx+2), jdx:(jdx+2));
res = masky.*bwsquare;
OUT(idx,jdx)=sum(sum(res));
end
end
Gy = OUT;
figure()
imshow(Gy);
G = sqrt(Gxdbl.^2 + Gydbl.^2);
figure()
imshow(G);

binary mask for the coordinates using MATLAB

I need to create a binary mask. I have some coordinates and I make those coordinates and inside that region equal to 1, and background equal to zero.
Here is what I have done, but the problem is ROI located not in the correct position and located on the right bottom of the image. I appreciate if someone could point me to the right direction.
function [X,Y, BW] = Create_mask(X,Y,im)
X = round(X);
Y = round(Y);
X ( X < 1 ) = 1;
Y ( Y < 1 ) = 1;
BW = im > 255;
for p = 1:length(X)
BW(Y(p),X(p)) = 1;
end
for n = 0:(1/round(sqrt((X(end)-X(1))^2 + (Y(end)-Y(1))^2 ))):1
xn = round(X(1) +(X(end) - X(1))*n);
yn = round(Y(1) +(Y(end) - Y(1))*n);
BW(yn,xn) = 1;
end
se = strel('disk',10);
BW = imclose(BW,se);
BW = imdilate(BW,se);
BW = imfill(BW,'holes');
im( im < 255 ) = 0;
im = imclose(im,se);
BW = BW * 255;
BW = im2uint8(BW);
% BW = imresize(BW, [256 256],'nearest');
figure;
imshow(BW);
% close all;
end
Here is the output the function:
I was expecting to be similar to this image. This is not the exact solution but it shows my expectation.
X and Y coordinates are attached here, The first col is X and the second Y.
you can do this by calling function inpolygon, try this
function mask=createmask(x,y, cmin, cmax, dx)
if(nargin<3)
cmin=min([x(:) y(:)]);
end
if(nargin<4)
cmax=max([x(:) y(:)]);
end
if(nargin<5)
dx=(cmax-cmin)/100;
end
if(length(dx)==1)
dx=[dx dx];
end
[xi,yi]=meshgrid(cmin(1):dx(1):cmax(1),cmin(2):dx(2):cmax(2));
mask=reshape(inpolygon(xi(:),yi(:),x(:),y(:)), size(xi));
to test
xv = [0 3 3 0 0 NaN 1 1 2 2 1];
yv = [0 0 3 3 0 NaN 1 2 2 1 1];
mask=createmask(xv,yv, [-1 -1], [4 4]);
imagesc(mask)

How to append data table below a plot in matlab?

I'm trying to make a compound plot in matlab, with a data table below. Just like the one in this image (yes, that one was made in excel):
As far as I go, I'm able to make the plot, but have no idea of how to make the table below. Here's my code:
y = [1,4; 0,0; 0,0; 1,0; 4,5; 21,10; 13,9; 3,3; 2,NaN; 0,NaN; 0,NaN; 1,NaN];
z = [16,34; 16,17; 26,17; 27,21; 42,37; 60,45; 45,47; 37,33; 28,NaN; 14,NaN;
16,NaN; 21,NaN];
z(z==0) = nan;
aa=max(y);
P= max(aa);
bb=max(z);
q= max(bb);
yyaxis left
a=bar(y,1,'EdgeColor','none');
ylabel('Días');
ylim([0 (P+2)]);
yyaxis right
b=plot(z);
ylim([0 (q+5)]);
ylabel('µg/m³');
b(1).LineWidth = 2;
b(1).Marker = 's';
b(1).MarkerFaceColor = [1 0.5216 0.2];
b(2).Marker = 'o';
b(2).MarkerFaceColor = [0 0.5255 0.9020];
b(2).LineWidth = 2;
b(2).Color = [0 0.4392 0.7529];
XTickLabel={'Enero' ; 'Febrero' ; 'Marzo'; 'Abril' ; 'Mayo' ; 'Junio' ;
'Julio' ; 'Agosto' ; 'Septiembre' ; 'Octubre' ; 'Noviembre' ;
'Diciembre'};
XTick=[1:12];
set(gca, 'XTick',XTick);
set(gca, 'XTickLabel', XTickLabel);
set(gca, 'XTickLabelRotation', 45);
set(gcf, 'Position', [100, 100, 1000, 350])
%Maximizar el espacio de la figura
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
%%%%%% Grilla %%%%%%%
grid on
legend('Total Episodios 2017','Total Episodios 2018','Conc.Prom. Mensual
2017','Conc.Prom. Mensual 2018');
%%% Colores %%%%
barmap=[1 0.4 0; 0 0.4392 0.7529];
colormap(barmap);
I would deeply appreciate any help you could give me.
figure;
% Plot first part
subplot(2,1,1);
x = [2 3 4 1 2 4 12 45];
plot(x)
% Plot table
ha = subplot(2,1,2);
pos = get(ha,'Position');
un = get(ha,'Units');
ht = uitable('Units',un,'Data',randi(100,10,3), 'Position',pos);

MATLAB - stacked bar plot with negative value in GUI

I need to make stacked bar plot with my data to look like this:
Graph1
My data set:
data1 = [0 0 3.16 25.08 46.87 57.97 39.25 28.81 10.63 0.06 0 0]
data2 = [74.00 152.68 319.99 514.05 635.73 647.61 645.32 569.51 398.48 226.13 84.88 52.08]
data3 = [628.07 497.66 426.97 285.56 220.67 184.04 212.71 239.93 318.25 451.61 545.02 626.39]
If I make it like this:
x = 1:12;
y = [data2' data1' data3'];
bar(handles.axes1,x,y,'stacked')
It looks like this:
Graph2
So I need to green part be like negative values on the first graph (blue values)
If I do it like this in classic workspace:
x = 1:12;
y1 = [data2' data3'];
bar(x,y1,'stacked')
hold on
bar(x, -data1, 'g')
hold off
It looks as I want, but if I do it this way in GUI
x = 1:12;
y1 = [data2' data3'];
bar(handles.axes1,x,y1,'stacked')
hold on
bar(handles.axes1,x, -data1, 'g')
hold off
Only negative values are plotted.
Thank you for advice.
All you need is specifying the handle you are operating with in hold
clear;clc;close all
data1 = [0 0 3.16 25.08 46.87 57.97 39.25 28.81 10.63 0.06 0 0]
data2 = [74.00 152.68 319.99 514.05 635.73 647.61 645.32 569.51 398.48 226.13 84.88 52.08]
data3 = [628.07 497.66 426.97 285.56 220.67 184.04 212.71 239.93 318.25 451.61 545.02 626.39]
x = 1:12;
y1 = [data2' data3'];
ax = axes;
figure % some other figures that interferes gcf()
bar(ax,x,y1,'stacked')
hold(ax,'on')
bar(ax,x, -data1, 'g')
hold(ax,'off')

Add different color line plots to gscatter plots in for loop

I have the following code:
limits = [-1 1 -1 1];
g1 = [1;2;3;4;5;6;7;8];
col = [1 1 0; 1 0 1; 0 1 1; 1 0 0; 0 1 0; 0 0 1; 0 0 0; 0.5 0.3 0.1];
sym = 'vvvv^^^^';
sym2 = 'xxxxxxxx';
points = 30;
for i = 1:8;
mhuR(i,1) = mean(HSRXdistpR(i,:));
mhuR(i,2) = mean(HSRYdistpR(i,:));
mhuL(i,1) = mean(HSRXdistpL(i,:));
mhuL(i,2) = mean(HSRYdistpL(i,:));
mhuX(i,1) = mean(TotxcomHSRpX(i,:));
mhuX(i,2) = mean(TotxcomHSRpY(i,:));
CR{i} = cov(HSRXdistpR(i,:),HSRYdistpR(i,:));
CL{i} = cov(HSRXdistpL(i,:),HSRYdistpL(i,:));
CX{i} = cov(TocomXdistp(i,:),TocomYdistp(i,:));
ellipR{i} = uncertEllip(CR{i},mhuR(i,:),points);
ellipL{i} = uncertEllip(CL{i},mhuL(i,:),points);
ellipX{i} = uncertEllip(CX{i},mhuX(i,:),points);
end
figure; hold on
scatter(HSRXdistbR2,HSRYdistbR2,'ko'); hold on
scatter(HSRXdistbL2,HSRYdistbL2,'ko'); hold on
scatter(TocomXdistb2,TocomYdistb2,'kx'); hold on
gscatter(HSRXp2(:,1),HSRYp2(:,1),g1,col,sym), hold on
gscatter(HSRXp2(:,2),HSRYp2(:,2),g1,col,sym), hold on
gscatter(copHSRXp2(:,2),copHSRYp2(:,2),g1,col,sym2), hold on
for i = 1:8;
plot(ellipR{i}(:,1),ellipR{i}(:,2),col(i,:)), hold on
plot(ellipL{i}(:,1),ellipL{i}(:,2),col(i,:)), hold on
plot(ellipX{i}(:,1),ellipX{i}(:,2),col(i,:)), hold on
end
vline(0)
hline(0)
legend('base','base','base','-0.04m', '-0.08m', '-0.12m', '-0.16m', '0.04m', '0.08m', '0.12m', '0.16m');
title({'xCoM and left and right foot placement relative',...
'to xCoM per perturbation, for all subjects'})
axis(limits)
xlabel('x displacement (scaled)');
ylabel('y displacement (scaled)');`
I'm trying to get the line plots (second for loop) to have the same colors as the scatter plot data (the uncertainty ellipses each belong to their own scatter point). However, it won't allow me to use the colors from the col matrix. Am I missing something here?
Error:
Data must be a single matrix Y or a list of pairs X,Y.
Not trying to define the colors works fine, but of course the colors don't match.
Try replacing each of those erroring lines with this:
plot(ellipR{i}(:,1),ellipR{i}(:,2),'Color',col(i,:));