Info
此问题已关闭。 请重新打开它进行编辑或回答。
for loops for multiple figures
1 次查看(过去 30 天)
显示 更早的评论
Hello, I would like to run my code in that way, that it generates multiple figures with two for loops.
At first I define many constants. After that I define my Titles and my axis length. Afterwards I calculate my data. I save the date in every step in an cell matrix named Results. After doing that, I want to plot my figures. The data comes out of the varialbe Results. I extract the cell entries with cell2mat. Two for loops are helping me doint that.
But the code does not work.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NOT IMPORTANT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Constants
% Main
t = 4; % Amount of pictures
g = 1; % Gain middle ear
b = 0.4; % Relation of displacement ?_b zu ?_p
gamma = 1; % Gain of OHC (for death ear: gamma = 0) (default value: gamma = 1)
rho = 1; % Density of cochlea fluid, unit (dyn*cm^-3)
nf = 5; % Amount of curves in the figure, test frequencies
% Dimension of Cochlea
L = 3.5; % Length of Cochlea, unit (cm)
H = 0.1; % Heigth of Cochlea, unit (cm)
W = 0.1; % Width of Cochlea, unit (cm)
N = 251; % Amount of Cochlea partitions
% Arrays for data
x = transpose(linspace(0,L,N)); % Vector of distance for Cochlea partitions
o = ones(N-1,1);
P = zeros(N,nf); % Vector of pressure for Cochlea partitions, unit (dyn*cm^-2)
Yb = zeros(N,nf); % Vector of admittance for Cochlea partitions, unit (dyn*s*cm^-3)
Db = zeros(N,nf); % Vector of BM displacement for Cochlea partitions, unit (cm)
Dh = zeros(N,nf); % Vector of OHC displacement for Cochlea partitions, unit (cm)
% BM: stiffness, damping und mass
k1 = 1.1e9*exp(-4*x); % stiffness, unit (dyn*cm^-3)
c1 = 20+1500*exp(-2*x); % damping, unit (dyn*s*cm^-3)
m1 = 0.003; % mass, unit (g*cm^-2)
k2 = 7e6*exp(-4.4*x);
c2 = 10*exp(-2.2*x);
m2 = 0.0005;
k3 = 1e7*exp(-4*x);
c3 = 2*exp(-0.8*x);
k4 = 6.15e8*exp(-4*x);
c4 = 1040*exp(-2*x);
% Middle ear: stiffness, damping und mass
km = 2.1e5;
cm = 400;
mm = 0.045;
As = 0.01; % Area of the stapes, unit (cm^2)
Am = 0.35;
Gm = 0.5;
Pe = 2.848e-4; % Pressure of the ear drum, unit (dyn*cm^-2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HERE IT STARTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define titles and axis length
devTitle = {'Pressure','Impedance','BM displacement', 'OHC displacement'};
C = {[0 L -120 0],[0 L -1 1];
[0 L -100 20], [0 L -5 1];
[0 L -40 80], [0 L -5 1];
[0 L -100 20],[0 L -5 1];
};
%%Compute data
f = 50;
dx = L/(N-1); % Equation A1
Ap = b/g;
for J = 1:nf,
f = f*3;
s = 2i*pi*f;
srd = 2*s*rho*dx;
% Impedance
z1 = k1/s+c1+m1*s;
z2 = k2/s+c2+m2*s;
z3 = k3/s+c3;
z4 = k4/s+c4;
Zb = z1+z2.*(z3-gamma*z4)./(z2+z3);
Zp = Zb/Ap; % Equation 12
hc = z2./(z2+z3);Q = zeros(N,1);
% Middle ear
Zm = km/s+cm+mm*s;
ame = -srd*As/(W*H*Zm); % Equation A2
pme = ame*(Am/(Gm*As))*Pe; % Equation A3
% Solution of pressure for middle ear
Q = zeros(N,1);
Q(1) = pme;
a = (s*rho*dx*dx/(H))*(Zp.^(-1));
A = diag(2+a,0)+diag(-o,1)+diag(-o,-1);
A(1,1) = 1+ame;
p = A\Q;
P(:,J) = p/Pe;
Yb(:,J) = Zb.^(-1);
Db(:,J) = ((p./(Zb*g))./s)*1e7;
Dh(:,J) = hc.*Db(:,J);
% Create list for legend
list{J} = [num2str(f) ' Hz'];
% Combine results for automated plotting
for i = 1:t;
if i == 1
Results{i,J} = P(:,J);
elseif i == 2
Results{i,J} = Yb(:,J);
elseif i == 3
Results{i,J} = Db(:,J);
else
Results{i,J} = Dh(:,J);
end
end
end
xx = x;
tpi = 2*pi;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plotting data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
J=1;
i=1
for J = 1:nf;
for i = 1:t
%%Print Figures
figure(i);clf;
% Plot data
ax1 = subplot(211);
plot(xx,real(20*log10(cell2mat(Results(i,J)))));
axis(cell2mat(C(i,1)));
title(devTitle(i));
ax2 = subplot(212);
plot(xx,unwrap(imag(log(cell2mat(Results(i,J)))))/tpi);
axis(cell2mat(C(i,2)));
% Set Ylabel for subplots
hLabel1 = ylabel(ax1,'Amplitude in dB');
hLabel2 = ylabel(ax2,'Phase in \pi');
pos1 = get(hLabel1, 'Position');
pos2 = get(hLabel2, 'Position');
newX = min(pos1(1), pos2(1));
pos1(1) = newX;
pos2(1) = newX;
set(hLabel1, 'Position', pos1);
set(hLabel2, 'Position', pos2);
% Set axis value
LY1 = get(ax1,'YLim');
Bildhoehe1 = get(gcf);
NumTicksY1 = abs((LY1(1)-LY1(2))/20);
set(ax1,'YTick',linspace(LY1(1),LY1(2),NumTicksY1+1));
LY2 = get(ax2,'YLim');
Bildhoehe2 = get(ax2, 'position');
NumTicksY2 = abs((LY2(1)-LY2(2))/1);
set(ax2,'YTick', linspace(LY2(1),LY2(2),NumTicksY2+1));
% Xlabel here
xlabel('Displacement in cm');
% Legend here
AX = legend(list,'Location','southoutside', 'orientation', 'horizontal');
% Set figure heigth
AX.Position(1:2) = [0.08, 0.0] ; % Move legend down and center a bit.
ax2.Position(4) = ax1.Position(4) ; % Set height axes 2 = height axes 1.
ax1.Position(2) = ax1.Position(2)+0.01 ; % Move axes 1 up a notch.
ax2.Position(2) = ax2.Position(2)-0.11 ; % Move axes 2 down.
2 个评论
KL
2017-10-17
What do you mean "the code does not work"?
I ran your code and I got 4 figures with 2 subplots on each.
Stephen23
2017-10-18
@Henrik Schädlich: your thread is unreadable, which makes it hard for anyone to help you. Please:
- Delete the code from the question and upload it by clicking the paperclip button.
- Delete the "answers" that are not answers and add them as comments to your original question.
回答(4 个)
Henrik Schädlich
2017-10-17
编辑:Henrik Schädlich
2017-10-17
1 个评论
Jan
2017-10-17
@Henrik: I cannot follow you. Are these answers or parts of the question? Do the huge number of repetitions help to understand something? The posted code is huge and this reduces the chance to get help massively. Can you narrow down your problem?
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!