Related
I have been trying to plot two images side by side with two sliders using uicontrol. I am relatively new to uicontrol using matlab. Only the second subplot updates when I change the slider.
close all;
clear all;
clc;
set(0,'defaultAxesFontSize',14)
set(0,'DefaultFigureWindowStyle','docked')
data = rand(3,1024,64,20);
nslice = size(data,4);
nidx = size(data,1);
if nslice == 1
sampling = 1; % in some of my data nslice would be 1 so I would like my slider to not give me error in such a case
else
sampling = 5;
end
idx = 1;
slice = 1;
h.f = figure(1);
x_axis = linspace(0,0.5,size(data,3));
y_axis = linspace(0,70,size(data,2));
set(h.f,'doublebuffer','on')
h.data = data;
inst_data = squeeze(data(nidx,:,:,slice));
h.ax(1) = subplot(1,2,1,'Parent',h.f,'Units','Normalized');
imagesc(h.ax(1),x_axis,y_axis, inst_data)
colorbar;
colormap(h.ax(1),'gray')
caxis(h.ax(1),[min(abs(inst_data(:))),max(abs(inst_data(:)))])
xlabel('xaxis')
ylabel('yaxis')
title('figure 1')
h.ax(2) = subplot(1,2,2,'Parent',h.f,'Units','Normalized');
log_data = log(inst_data);
imagesc(h.ax(2),x_axis,y_axis, log_data)
colorbar;
colormap(h.ax(2),'jet')
caxis(h.ax(2),[-max(abs(log_data(:))),max(abs(log_data(:)))])
xlabel('xaxis')
ylabel('yaxis')
title('figure 2')
sgtitle({'Sample data ', ['Slice: ',num2str(slice),' index: ', num2str(idx)]},'FontSize',16,'FontWeight','Bold')
h.slider1=uicontrol('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.06 0.8 0.05],...
'Style','slider',...
'SliderStep',[1,sampling],...
'Min',1,'Max',nslice,'Value',1,...
'Callback',{#slider_Callback,data,x_axis,y_axis});
txt1 = 'Slice';
txt2 = 'Index';
annotation('textbox', [0.05 0.07 0.05 0.04], 'string', txt1);
annotation('textbox', [0.05 0.021 0.05 0.04], 'string', txt2);
h.slider2=uicontrol('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.02 0.8 0.05],...
'Style','slider',...
'SliderStep',[1,1],...
'Min',1,'Max',nidx,'Value',1,...
'Callback',{#slider_Callback,data,x_axis,y_axis});
guidata(h.f,h)
%%
function slider_Callback(hObject,eventdata,data,x_axis,y_axis)%#ok<INUSD>
h=guidata(hObject);%retrieve struct
slice_i = round(get(h.slider1, 'Value'));
idx_i = round(get(h.slider2,'Value'));
inst = squeeze(data(idx_i,:,slice_i));
colorbar;
colormap(h.ax(1),'gray')
caxis(h.ax(1),[min(abs(inst(:))),max(abs(inst(:)))])
h.ax(1) = imagesc(x_axis,y_axis,inst); colorbar
lg = log(inst);
colorbar;
colormap(h.ax(2),'jet')
caxis(h.ax(2),[-max(abs(lg(:))),max(abs(lg(:)))])
h.ax(2) = imagesc(x_axis,y_axis, lg ); colorbar;
sgtitle({'Sample data ', ['Slice: ',num2str(slice_i),' index: ', num2str(idx_i)]},'FontSize',16,'FontWeight','Bold')
drawnow
end
Other problems include:
The slider slides with a floating value which shouldnt be the case
When my data has only 1 slice, the slider moves to value zero which shouldn't happen. You can try the same example with data = rand(3,1024,64,1);
I want to create a relative axis in Matlab like the $\Delta I$-rulers in the following plot.
Before I start writing up a function that constructs it manually, I would like to know if there's way of creating an object with the NumericRuler-properties (like the default axes of a figure())
So I ended up using the link provided by Sardar Usama's comment as inspiration and wrote a function to create an axes-object relative to the values of a "parent"-axes:
function ax = create_value_axes(hAx, pos)
%% ax = create_value_axes(hAx, pos)
%
% Create axes at the value points of hAx.
%
% pos(1) = x-position
% pos(2) = y-position
% pos(3) = x-width
% pos(4) = y-width
%
% Get "parent" position and value limits
hAx_pos = hAx.Position;
hAx_xlm = hAx.XLim;
hAx_ylm = hAx.YLim;
% Get relative position increment pr value increment
x_step = hAx_pos(3) / (hAx_xlm(2) - hAx_xlm(1));
y_step = hAx_pos(4) / (hAx_ylm(2) - hAx_ylm(1));
% Set position
subaxes_abs_pos(1) = (pos(1)-hAx_xlm(1)) * x_step + hAx_pos(1);
subaxes_abs_pos(2) = (pos(2)-hAx_ylm(1)) * y_step + hAx_pos(2);
subaxes_abs_pos(3) = pos(3) * x_step;
subaxes_abs_pos(4) = pos(4) * y_step;
% Create axes
ax = axes('Position', subaxes_abs_pos);
% Remove background
ax.Color = 'none';
end
Sidenote: I found that I didn't need plotboxpos to get the correct positions of the "parent"-axes, using Matlab r2019b on macOS Mojave 10.14.6
Anyway, this is what I end up with:
Using the code:
% Just some random data
mockup_data_ild = [-10 -7 -4 0 4 7 10];
mockup_data_itd_45 = [-40 -20 -10 0 10 20 40];
mockup_data_itd_60 = [-30 -15 -5 0 5 15 30];
% Create figure
figure('Color', 'w')
x_axis_offset = [0 30];
hold on
% Plot 45 dB result
p1 = plot_markers(x_axis_offset(1) + mockup_data_ild, mockup_data_itd_45, ii);
% Plot 60 dB results
p2 = plot_markers(x_axis_offset(2) + mockup_data_ild, mockup_data_itd_60, ii);
p2.Color = p1.Color;
p2.HandleVisibility = 'off';
hold off
% Set axes properties
ax = gca;
ax.XAxis.TickValues = [x_axis_offset(1) x_axis_offset(2)];
ax.XAxis.TickLabels = {'45 dB' '60 dB'};
ax.XAxis.Limits = [x_axis_offset(1)-15 x_axis_offset(2)+15];
ax.XAxisLocation = 'top';
ax.YAxis.Limits = [-80 100];
ax.YAxis.Label.String = 'Interaural Time Difference, \Deltat, in samples';
ax.YGrid = 'on';
% Create 45 dB axis
ax2 = create_DeltaI_axis(ax, x_axis_offset(1));
% Create 60 dB axis
ax3 = create_DeltaI_axis(ax, x_axis_offset(2));
% Create legend
leg = legend(ax, {'P1'});
leg.Location = 'northwest';
%% Helpers
function ax = create_DeltaI_axis(hAx, x_pos)
y_pos = -70;
y_height = 170;
range = 20;
ax = create_value_axes(hAx, [x_pos-range/2 y_pos range y_height]);
ax.XAxis.TickValues = [0 .25 .5 .75 1];
ax.XAxis.TickLabels = {'-10'
'-5'
'0'
'5'
'10'};
ax.XAxis.Label.String = '\DeltaI';
ax.XGrid = 'on';
ax.XMinorGrid = 'on';
ax.YAxis.Visible = 'off';
end
function p = plot_markers(x, y, ii)
markers = {'square','^', 'v', 'o', 'd'};
p = plot(x, y);
p.LineWidth = 1.5;
p.LineStyle = 'none';
p.Marker = markers{ii};
end
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);
Good evening,
I have a code working just fine as follows:
r=1.6;
M=0.000207;
D=-0.0256;
kappa=0.5;
gamma=20;%\W\km
Pp=0.2;
L=462.5;
SQ=sqrt(-(0.5*D)^2 +M);
z=0:0.1:L;
A=kappa*SQ.*(z-L);
B=((sqrt(M)/r)+(D/2))/(SQ);
C=EA(B);
E=SQ*cot(A+C);
Pminus=E-(D/2);
Pplus=M./Pminus;
P0=Pplus+Pminus+D;
CST1=Pplus.*Pminus;
CST2=P0-Pplus-Pminus;
dP0dz=kappa.*P0.*(Pplus-Pminus);
figure
set(gca,'fontsize',18)
plot(z,Pplus*1000, 'b',z,Pminus*1000,'r',z,P0*1000,'k','Linewidth',4);
xlabel('z(m)')
ylabel('Power (mW)')
legend('P_+','P_-','P_0','D','M','r','Orientation','horizontal')
%str = sprintf('D = %.4f , M = %.9f, and r =%.3f ',D,M,r);
%title(str);
xlim([0 L])
N=gamma.*Pp;%gamma Pp with units \km
y1=0.5*kappa.*1000*(Pplus+Pminus);
y2 =y1./N;
figure
hax=axes;
[ax,p1,p2] = plotyy(z,y1,z,y2,'plot','plot');
set(ax,{'ycolor'},{'k';'k'})
set(ax,{'fontsize'},{18;18})
set(p1,'linewidth',4)% to change the first line
set(p2,'linewidth',4) % to change the second line
set(p1,'Color','b')% to change the first line
set(p2,'Color','b') % to change the second line
%str = sprintf('D = %.6f ',D);
%title(str);
ylabel(ax(1),'K_{SBS}(km^{-1})','fontsize',18,...
'Color','k') % label left y-axis
ylabel(ax(2),'K_{SBS}( \gamma P )','fontsize',18,...
'Color','k') % label right y-axis
xlabel(ax(2),'z(m)','fontsize',18,...
'Color','k')% label x-axis
set(ax(1),'XLim',[0 L])
set(ax(2),'XLim',[0 L])
set(ax(1),'YLim',[min(y1) max(y1)])
set(ax(2),'YLim',[min(y2) max(y2)])
zz1=(max(y1)-min(y1))./4;
zz2=(max(y2)-min(y2))./4;
set(ax(1),'YTick',min(y1):zz1: max(y1) )
set(ax(2),'YTick',min(y2):zz2: max(y2) )
legend('K_{SBS}(km^{-1})')
% figure
% set(gca,'fontsize',18)
% plot(z,1000*dP0dz,'Linewidth',4);
% xlabel('z(m)')
% ylabel('dP_0/dz')
% xlim([0 L])
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=gamma.*Pp;%\km
lamdazero=1550;
lamdapump=linspace(1535,1560,100000);
lamdasignal=1545;
beta3=0.06;
beta4=-1*10^-4;
c=2*pi*299792458/1000;
A0=(1./lamdapump) -(1./lamdazero);
B0=(1./lamdapump) -(1./lamdasignal);
Third0=beta3.*(c.^3).*A0.*(B0.^2);
Fourth0=beta4.*(1./2).*c.^4.*(A0.^2).*(B0.^2);
Fourorder=c.^4.*beta4.*(1/12).*(B0).^4;
deltabeta=Third0+Fourth0+Fourorder;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%NO SBS
total0=deltabeta+(2.*N);
g0=((sqrt((N.^2)-(total0./2).^2)));
Gain0=((((N./(g0)).^2).*(sinh(g0.*L/1000)).^2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
g0=1.1*10^-11;
Aeff=1.1*10^-11;
PSBS=0.016;
SBS=(g0.*PSBS.*1000)./Aeff;
totalsbs=deltabeta+(2.*N)-SBS/2;
gsbs=((sqrt((N.^2)-(totalsbs./2).^2)));
Gainsbs=((((N./(gsbs)).^2).*(sinh(gsbs.*L/1000)).^2));
n=35;
first=round(1*(L/n));
step=round(L/n);
last=L;
for jj=1:length(deltabeta)
M = eye(2);
for ii = first:step:last;
tsv=deltabeta(jj)+(2*N)-(y1(z==(ii-round(0.5*(L/n)))));
gsv=((sqrt((N.^2)-(tsv./2).^2)));
F1=1i*tsv;
F2=2*gsv;
F3=F1./F2;
F4=1i*(N./gsv);
M1=cosh(gsv.*(step/1000))+(F3.*sinh(gsv.*(step/1000)));
M2=F4.*sinh(gsv.*(step/1000));
M3=-F4.*sinh(gsv.*(step/1000));
M4=cosh(gsv.*(step/1000))-(F3.*sinh(gsv.*(step/1000)));
M=M*[M1 M2;M3 M4];
TeRm=P0(z==ii)/P0(z==(ii-step));
M=[sqrt(TeRm) 0;0 1]*M;
end
Gv(1,jj)=M(1,1).*conj(M(1,1));
end
FigHandle = figure;
figureh=plot(lamdapump,10*log10(1+Gain0),'b',lamdapump,10*log10(1+Gainsbs),'g',lamdapump,10*log10(Gv),'r','Linewidth',5);
hold on
xlabel('\lambda_P (nm)','Fontsize',18)
ylabel('Signal Gain (dB)','Fontsize',18)
legend('Without SBS',' With SBS ideal ',' With SBS actual')
% title('Gain curves with SBS','fontsize',18)
set(gca,'XTick',1536:3:1560)
set(gca,'fontsize',18)
ylim([0 12])
hold off
My issue is that when changing the parameter L into a value smaller than one like with some changes in the parameters:
r=1.8;
M=0.80322;
D=-1.69;
kappa=700/4.6;
gamma=5285;%\W\km
Pp=7.6;
L=0.045333;
SQ=sqrt(-(0.5*D)^2 +M);
z=0:0.00001:L;
A=kappa*SQ.*(z-L);
B=((sqrt(M)/r)+(D/2))/(SQ);
C=EA(B);
E=SQ*cot(A+C);
Pminus=E-(D/2);
Pplus=M./Pminus;
P0=Pplus+Pminus+D;
CST1=Pplus.*Pminus;
CST2=P0-Pplus-Pminus;
dP0dz=kappa.*P0.*(Pplus-Pminus);
figure
set(gca,'fontsize',18)
plot(z,Pplus*1000, 'b',z,Pminus*1000,'r',z,P0*1000,'k','Linewidth',4);
xlabel('z(m)')
ylabel('Power (mW)')
legend('P_+','P_-','P_0','D','M','r','Orientation','horizontal')
%str = sprintf('D = %.4f , M = %.9f, and r =%.3f ',D,M,r);
%title(str);
xlim([0 L])
N=gamma.*Pp;%gamma Pp with units \km
y1=0.5*kappa.*1000*(Pplus+Pminus);
y2 =y1./N;
figure
hax=axes;
[ax,p1,p2] = plotyy(z,y1,z,y2,'plot','plot');
set(ax,{'ycolor'},{'k';'k'})
set(ax,{'fontsize'},{18;18})
set(p1,'linewidth',4)% to change the first line
set(p2,'linewidth',4) % to change the second line
set(p1,'Color','b')% to change the first line
set(p2,'Color','b') % to change the second line
%str = sprintf('D = %.6f ',D);
%title(str);
ylabel(ax(1),'K_{SBS}(km^{-1})','fontsize',18,...
'Color','k') % label left y-axis
ylabel(ax(2),'K_{SBS}( \gamma P )','fontsize',18,...
'Color','k') % label right y-axis
xlabel(ax(2),'z(m)','fontsize',18,...
'Color','k')% label x-axis
set(ax(1),'XLim',[0 L])
set(ax(2),'XLim',[0 L])
set(ax(1),'YLim',[min(y1) max(y1)])
set(ax(2),'YLim',[min(y2) max(y2)])
zz1=(max(y1)-min(y1))./4;
zz2=(max(y2)-min(y2))./4;
set(ax(1),'YTick',min(y1):zz1: max(y1) )
set(ax(2),'YTick',min(y2):zz2: max(y2) )
legend('K_{SBS}(km^{-1})')
% figure
% set(gca,'fontsize',18)
% plot(z,1000*dP0dz,'Linewidth',4);
% xlabel('z(m)')
% ylabel('dP_0/dz')
% xlim([0 L])
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=gamma.*Pp;%\km
lamdazero=1556;
lamdapump=linspace(1300,1800,100000);
lamdasignal=1536;
beta3=1.26;
beta4=0;
c=2*pi*299792458/1000;
A0=(1./lamdapump) -(1./lamdazero);
B0=(1./lamdapump) -(1./lamdasignal);
Third0=beta3.*(c.^3).*A0.*(B0.^2);
Fourth0=beta4.*(1./2).*c.^4.*(A0.^2).*(B0.^2);
Fourorder=c.^4.*beta4.*(1/12).*(B0).^4;
deltabeta=Third0+Fourth0+Fourorder;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%NO SBS
total0=deltabeta+(2.*N);
g0=((sqrt((N.^2)-(total0./2).^2)));
Gain0=((((N./(g0)).^2).*(sinh(g0.*L/1000)).^2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
g0=0.7*10^-9;
Aeff=2.3*10^-12;
PSBS=Pp*4/57.5;
SBS=(g0.*PSBS.*1000)./Aeff;
totalsbs=deltabeta+(2.*N)-SBS/2;
gsbs=((sqrt((N.^2)-(totalsbs./2).^2)));
Gainsbs=((((N./(gsbs)).^2).*(sinh(gsbs.*L/1000)).^2));
n=35;
first=(1*(L/n));
step=(L/n);
last=L;
for jj=1:length(deltabeta)
M = eye(2);
for ii = first:step:last;
tsv=deltabeta(jj)+(2*N)-(y1(z==(ii-(0.5*(L/n)))));
gsv=((sqrt((N.^2)-(tsv./2).^2)));
F1=1i*tsv;
F2=2*gsv;
F3=F1./F2;
F4=1i*(N./gsv);
M1=cosh(gsv.*(step/1000))+(F3.*sinh(gsv.*(step/1000)));
M2=F4.*sinh(gsv.*(step/1000));
M3=-F4.*sinh(gsv.*(step/1000));
M4=cosh(gsv.*(step/1000))-(F3.*sinh(gsv.*(step/1000)));
M=M*[M1 M2;M3 M4];
TeRm=P0(z==ii)/P0(z==(ii-step));
M=[sqrt(TeRm) 0;0 1]*M;
end
Gv(1,jj)=M(1,1).*conj(M(1,1));
end
FigHandle = figure;
figureh=plot(lamdapump,10*log10(1+Gain0),'b',lamdapump,10*log10(1+Gainsbs),'g',lamdapump,10*log10(Gv),'r','Linewidth',5);
hold on
xlabel('\lambda_P (nm)','Fontsize',18)
ylabel('Signal Gain (dB)','Fontsize',18)
legend('Without SBS',' With SBS ideal ',' With SBS actual')
title('Gain curves with SBS','fontsize',18)
%set(gca,'XTick',1300:10:1800)
set(gca,'fontsize',18)
%ylim([0 12])
hold off
The code does not work. The reason in my opinion is that when using the function round it rounds to zero. If i remove the round function it no longer works. Any thoughts on how can I make my code work again?
Is there any chance to work around round function because i need to take n steps. However I simply can not do so because L is very small. I need the value of L to stay small.
My apologizes for the long codes and explanations. Be free to ask !]1
I am writing a code to produce a house with a given height, width, length, and angle of sloped roof.
I was able to produce the walls with surf by using a zero value for a specific dimension, allowing a wall to be made as an x-y plane, x-z plane, etc.
However, Now I am trying to produce a desk inside that is an x-y plane with a specific height, and cant figure out how..
MATLAB says it needs a matrix, but a matrix formed only from the height doesn't work, also using ndgrid(heightValue:.5:heightValue) doesn't work
If anyone knows how to help, I would greatly appreciate it.
Code:
(one of the desk sides is in the wrong place as well for similar problems, but help with this problem will allow me to figure out how to fix it)
clear; clc; close all;
%% INPUT VARIABLES
lengthdlg = 'Please enter length of base (m): ';
widthdlg = 'Please enter width of base (m): ';
heightdlg = 'Please enter height of wall (m): ';
angledlg = 'Please enter the angle of the roof (degrees): ';
dlgbox = inputdlg({lengthdlg, widthdlg, heightdlg, angledlg},...
'Simple AutoCAD', [1 50; 1 50; 1 50; 1 50], {'30','40','15','30'});
BaseL = str2double(dlgbox(1));
BaseW = str2double(dlgbox(2));
WallH = str2double(dlgbox(3));
RoofA = str2double(dlgbox(4));
m = tand(RoofA);
h = figure;
set(h,'name','Simple AutoCAD House','numbertitle','off')
xlabel('x'),ylabel('y'),zlabel('z')
title('Simple AutoCAD House')
colormap white
%% Base/Floor
[xB,yB] = ndgrid(0:1:BaseL, 0:1:BaseW);
zB = (xB*0);
surf(xB,yB,zB,'FaceColor','k')
hold on
%% Walls
%Front Wall (w/Door opening)
[xFW,zFW] = ndgrid(0:1:BaseL, 0:1:WallH);
yFW = (xFW*0);
yFW(xFW>.4*BaseL & xFW<.6*BaseL & zFW<.8*WallH) = nan;
surf(xFW,yFW,zFW);
hold on
%Back Wall
[xBW,zBW] = ndgrid(0:.5:BaseL, 0:.5:WallH);
yBW = (xBW*0)+BaseW;
surf(xBW,yBW,zBW);
%Right Wall
[yRW,zRW] = ndgrid(0:.5:BaseW, 0:.5:WallH);
xRW = (yRW*0)+BaseL;
surf(xRW,yRW,zRW);
%Left Wall
[yLW,zLW] = ndgrid(0:.5:BaseW, 0:.5:WallH);
xLW = (yLW*0);
xLW(yLW>.25*BaseW & yLW<.75*BaseW & zLW>.5*WallH & zLW<.7*WallH) = nan;
surf(xLW,yLW,zLW);
%% Roof
%Left Panel
[xLP,yLP] = ndgrid(0:.5:(BaseL/2), 0:.5:BaseW);
zLP = (m*xLP)+WallH;
surf(xLP,yLP,zLP)
hold on
%Right Panel
[xRP,yRP] = ndgrid((BaseL/2):.5:BaseL, 0:.5:BaseW);
zRP = (-m*xRP)+(WallH+m*BaseL);
surf(xRP,yRP,zRP)
%% Roof Triangles
%Front Triangle
[xFT,zFT] = ndgrid(0:.25:BaseL, WallH:.25:(m*BaseL/2)+WallH);
yFT = 0*xFT;
yFT(zFT>(m*xFT+WallH)) = nan;
yFT(zFT>(-m*xFT+(m*BaseL+WallH))) = nan;
surf(xFT,yFT,zFT)
%Back Triangle
[xBT,zBT] = ndgrid(0:.25:BaseL, WallH:.25:(m*BaseL/2)+WallH);
yBT = (xBT*0)+BaseW;
yBT(zBT>(m*xBT+WallH)) = nan;
yBT(zBT>(-m*xBT+(m*BaseL+WallH))) = nan;
surf(xBT,yBT,zBT)
%% Door
[xD,zD] = ndgrid(.4*BaseL:.5:.6*BaseL, 0:.5:.8*WallH);
yD = xD*0;
door = surf(xD,yD,zD,'FaceColor','g');
%% Windows
%Left Window
[yLWin,zLWin] = ndgrid(.25*BaseW:.5:.75*BaseW, .5*WallH:.5:.75*WallH);
xLWin = (yLWin*0);
surf(xLWin,yLWin,zLWin,'FaceAlpha',.25,'FaceColor','b')
%% Desk and chair
%desktop
[xDT,yDT] = ndgrid(.8*BaseL:.5:.99*BaseL, .7*BaseW:.5:.99*BaseW);
zDT = (0*xDT);
surf(xDT,yDT,zDT,'FaceColor','r')
%DeskNear
[xDN,zDN] = ndgrid(.8*BaseL:.5:BaseL, 0:.5:.25*WallH);
yDN=(0*zDN)
surf(xDN,yDN,zDN,'FaceColor','r')
%% Door Opening
k = waitforbuttonpress;
if k == 0
delete(door)
end
The ZData property of a surface, is the z coordinate for each point in the surface. In your example, you set it to the right size by making it the same size as your XData and YData; however, the way you construct your ZData is a little off. You essentially do this:
zDT = (0 * xDT);
What this effectively does, is forces the z coordinate of every point in the mesh to be 0 (i.e. on the floor). This is why the desk top simply sits on the floor, and also why the side of the desk sits on the side of the wall (y = 0).
To fix this, you want to instead calculate what the values should actually be rather than setting them to zero.
Desktop
The nice thing is that the z value for the entire desktop is actually a constant for the whole surface since it's parallel to the floor. You define what the height of the desktop is when you create the side of the desk.
[xDN, zDN] = ndgrid(.8*BaseL:.5:BaseL, 0:.5:.25*WallH);
Here you're saying the height of the desk is 0.25 * WallH rounded to the previous 0.5 (because of your step size). So we want to set the ZData of the desktop to be that value (the max of zDN) and we can multiply that value by a matrix of ones the size of the XData to make it the right size.
zDT = max(zDN(:)) * ones(size(xDT));
Side of Desk
For the side of the desk, the issue isn't the ZData anymore, it's now the YData. As it currently is, you have it set to all zeros so it get's stuck to the wall at y = 0.
yDN = (0 * zDN);
Again, we want the y value to be constant for the whole side of the desk. We can compute what this y value is by using the minimum y value from the desk top (again, making sure it's the right size by using a matrix of ones).
yDN = min(yDT(:)) * ones(size(xDN));
Summary
We can make those two changes and this makes the bottom portion of your code look something like this.
%desktop
[xDT,yDT] = ndgrid(.8*BaseL:.5:.99*BaseL, .7*BaseW:.5:.99*BaseW);
[xDN,zDN] = ndgrid(.8*BaseL:.5:BaseL, 0:.5:.25*WallH);
zDT = max(zDN(:)) * ones(size(xDT));
surf(xDT,yDT,zDT,'FaceColor','r')
%DeskNear
yDN = min(yDT(:)) * ones(size(xDN));
surf(xDN,yDN,zDN,'FaceColor','r')
If we apply that, we get the desktop off of the floor and the side of the desk where it belongs.
As Amro said in the comment to your post, it will likely be much easier to draw this sort of thing using patch rather than surf as it gives you a great deal more flexibility as far as shapes.
Here is my version of the house :)
Code should be easy to follow. Note that I'm using the patch function instead.
% input measurements
BaseL = 30;
BaseW = 40;
WallH = 15;
RoofA = 30;
% colors
m = tand(RoofA);
clr = hsv(10);
clf
% floor
patch([0 1 1 0].*BaseL, ...
[0 0 1 1].*BaseW, ...
[0 0 0 0].*WallH, clr(1,:));
% front wall (w/ door opening)
patch([1 1 0 0 0.4 0.4 0.6 0.6].*BaseL, ...
[0 0 0 0 0 0 0 0].*BaseW, ...
[0 1 1 0 0 0.8 0.8 0].*WallH, clr(2,:));
% back wall
patch([0 1 1 0].*BaseL, ...
[1 1 1 1].*BaseW, ...
[0 0 1 1].*WallH, clr(3,:));
% right wall
patch([1 1 1 1].*BaseL, ...
[0 0 1 1].*BaseW, ...
[0 1 1 0].*WallH, clr(4,:));
% left wall (w/ window opening)
patch([0 0 0 0 0 0 0 0 0 0 0 0].*BaseL, ...
[0 0 0.5 0.5 0.25 0.25 0.75 0.75 0.5 0.5 1 1].*BaseW, ...
[0 1 1 0.7 0.7 0.5 0.5 0.7 0.7 1 1 0].*WallH, clr(5,:));
% roof left/right panels
patch([0 0.5 0.5 0].*BaseL, ...
[0 0 1 1].*BaseW, ...
[0 1 1 0].*(m*BaseL/2)+WallH, clr(6,:));
patch([1 0.5 0.5 1].*BaseL, ...
[0 0 1 1].*BaseW, ...
[0 1 1 0].*(m*BaseL/2)+WallH, clr(7,:));
% roof front/back triangles
patch([0 1 0.5].*BaseL, ...
[0 0 0].*BaseW, ...
[0 0 1].*(m*BaseL/2)+WallH, clr(8,:));
patch([0 1 0.5].*BaseL, ...
[1 1 1].*BaseW, ...
[0 0 1].*(m*BaseL/2)+WallH, clr(9,:));
% door
hDoor = patch([0.4 0.6 0.6 0.4].*BaseL, ...
[0 0 0 0].*BaseW, ...
[0 0 0.8 0.8].*WallH, 'k', 'FaceAlpha',0.75);
% window on left wall
hWin = patch([0 0 0 0].*BaseL, ...
[0.25 0.75 0.75 0.25].*BaseW, ...
[0.5 0.5 0.7 0.7].*WallH, 'k', 'FaceAlpha',0.75);
% table inside
patch([0.8 0.99 0.99 0.8].*BaseL, ...
[0.7 0.7 0.99 0.99].*BaseW, ...
[0.2 0.2 0.2 0.2].*WallH, clr(10,:));
patch([0.8 0.99 0.99 0.8].*BaseL, ...
[0.7 0.7 0.7 0.7].*BaseW, ...
[0.01 0.01 0.2 0.2].*WallH, clr(10,:));
patch([0.8 0.99 0.99 0.8].*BaseL, ...
[0.99 0.99 0.99 0.99].*BaseW, ...
[0.01 0.01 0.2 0.2].*WallH, clr(10,:));
% 3D view
axis([0 BaseL 0 BaseW 0 WallH*2]), axis vis3d
view(3), grid on
xlabel('X'), ylabel('Y'), zlabel('Z')
title('Simple AutoCAD House')
% animate door/window transparencies
t = linspace(-pi,pi,20);
anim = #(t) 1-(tanh(t)+1)/2; % smooth function from 1 to 0
for i=1:numel(t)
set([hDoor,hWin], 'FaceAlpha',anim(t(i)))
pause(0.1)
end
There is a small animation at the end, where the door and window go from tinted to clear. Hope you like it!