Info

This question is closed. Reopen it to edit or answer.

for loops for multiple figures

1 view (last 30 days)
Henrik Schädlich
Henrik Schädlich on 17 Oct 2017
Commented: Stephen23 on 18 Oct 2017
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 Comments
KL
KL on 17 Oct 2017
What do you mean "the code does not work"?
I ran your code and I got 4 figures with 2 subplots on each.
Stephen23
Stephen23 on 18 Oct 2017
@Henrik Schädlich: your thread is unreadable, which makes it hard for anyone to help you. Please:
  1. Delete the code from the question and upload it by clicking the paperclip button.
  2. Delete the "answers" that are not answers and add them as comments to your original question.

Answers (4)

Henrik Schädlich
Henrik Schädlich on 17 Oct 2017
This is what I want to achieve. The code is attached below. The Figures are different from the code above to the code below. I am not able to integrate the loop properly. Do you have any advice for me?
I am trying to stop the double coding.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NOT IMPORTANT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Constants
% Main
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 ear drum, unit (dyn*cm^-2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HERE IT STARTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%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 preassure 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'];
end
xx = x;
tpi = 2*pi;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plotting data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Impedance
figure(1);clf;
% % plot data
ax1 = subplot(211); plot(xx,real(20*log10(Yb))); axis([0 L -120 0]);
title('Impedanz');
ax2 = subplot(212); plot(xx,unwrap(imag(log(Yb)))/tpi); axis([0 L -1 1]);
% 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('Abstand in cm');
% Legend here
AX = legend(list,'Location','southoutside', 'orientation', 'horizontal');
% Set figure heigth
AX.Position(1:2) = [0.08, 0.02] ; % 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.
%%BM displacement
figure(2);clf;
% plot data
ax1 = subplot(211); plot(xx,real(20*log10(Db))); axis([0 L -100 20]);
title('BM Auslenkung');
ax2 = subplot(212); plot(xx,unwrap(imag(log(Db)))/tpi); axis([0 L -5 1]);
% 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('Abstand in cm');
% Legend here
AX = legend(list,'Location','southoutside', 'orientation', 'horizontal');
% Set figure heigth
AX.Position(1:2) = [0.08, 0.02] ; % 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.
%%Pressure
figure(3);clf;
% plot data
ax1 = subplot(211);plot(xx,real(20*log10(P)));axis([0 3.5 -40 80]);
title('Druckverteilung');
ax2 = subplot(212);plot(xx,unwrap(imag(log(P)))/tpi);axis([0 3.5 -5 1]);
% 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('Abstand in cm');
% Legend here
AX = legend(list,'Location','southoutside', 'orientation', 'horizontal');
% Set figure heigth
AX.Position(1:2) = [0.08, 0.02] ; % 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.
%%OHC displacement
figure(4);clf;
% plot data
ax1 = subplot(211);plot(xx,real(20*log10(Dh)));axis([0 3.5 -100 20]);
title('Auslenkung der Haarzellen');
ax2 = subplot(212);plot(xx,unwrap(imag(log(Dh)))/tpi);axis([0 3.5 -5 1]);
% 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('Abstand in cm');
% Legend here
AX = legend(list,'Location','southoutside', 'orientation', 'horizontal');
% Set figure heigth
AX.Position(1:2) = [0.08, 0.02] ; % 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.

Henrik Schädlich
Henrik Schädlich on 17 Oct 2017
Edited: Henrik Schädlich on 17 Oct 2017
The four plots should be printed in a simple loop. I tried to integrate that but the result looks different from the first code to the second code. I hope you have an idea.
The error massage is:
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
Warning: Ignoring extra legend entries.
> In legend>set_children_and_strings (line 674)
In legend>make_legend (line 346)
In legend (line 294)
In english (line 156)
  1 Comment
Jan
Jan on 17 Oct 2017
@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?

Henrik Schädlich
Henrik Schädlich on 18 Oct 2017
Edited: Henrik Schädlich on 18 Oct 2017
The secund code block is the current one. It plots four figures on its own. There you have four sections startig each with figure(...). So it is figure(1) to figure(4).
The first code block is supposed to do the same. The Problem is the for-loop for plotting the figures. It does not work properly
The third code block is the error massage of the first code block.
I think i found the problem.
clear all;
close all;
%%Minimal example
N= 101; % steps for x-axis
L=1; % step size
n=6; % curves per figure, results matrix width
nf = 2; % amount of figures
t = 4; % results matrix hight
%%Get data
x(:,1) = linspace(0,L,N);
for i = 1:n;
y11(:,i) = linspace(-100,i*20,101);
y12(:,i) = linspace(-4,i,101);
y21(:,i) = linspace(-100,i*40,101);
y22(:,i) = linspace(-4,i*2,101);
% Generate data cell struct
for j = 1:t;
if j == 1
Results{j,i} = y11(:,i);
elseif j == 2
Results{j,i} = y12(:,i);
elseif j == 3
Results{j,i} = y21(:,i);
else
Results{j,i} = y22(:,i);
end
end
end
% Define axes
C = {[0 1 -100 100],[0 1 -5 0];
[0 1 -100 0], [0 1 -5 10];
};
%%Plot data
for J = 1:nf;
for I = 1:n;
figure(J)
ax1 = subplot(211);
plot(x,cell2mat(Results((2*J-1),I))); hold on;
ax2 = subplot(212);
plot(x,cell2mat(Results((2*J),I))); hold on;
end
axis(ax1, C{J,1});
axis(ax2, C{J,2});
end
I need to define the figures in a seperate for-loop. After the I-for-loop i can start defining the axes, the legend, the labels and so on. Doing that in the J-for-loop. Actually i am trying to do that all in the I-for-loop. I will try to transfer that to the first code block.
Additionally i messed up the definition of the axial limits. ymin is higher than ymax. Leds to an error massage to. ^^

Henrik Schädlich
Henrik Schädlich on 18 Oct 2017
I made it happening, thanks to you. This is the appropriate code.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plotting data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xx = x;
tpi = 2*pi;
J=1;
i=1;
for i = 1:t;
%%Print Figures
figure(i);
for J = 1:nf
% Plot data
ax1 = subplot(211);
%plot(xx,real(20*log10(cell2mat(Results(i,J))))); hold on;
plot(xx,real(20*log10(Results{i,J}))); hold on;
ax2 = subplot(212);
%plot(xx,unwrap(imag(log(cell2mat(Results(i,J)))))/tpi); hold on;
plot(xx,unwrap(imag(log(Results{i,J})))/tpi); hold on;
end
title(ax1, devTitle(i));
axis(ax1, C{i,1});
axis(ax2, 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.02] ; % 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.
end
You can see. There is only one entry for figure, not four like before. At firs I am opening one figure afterwards i am writing data into the figure and the I implement the style. Do you have an advise for me? Maype ther is a better solution.
Best regards Henrik

Community Treasure Hunt

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

Start Hunting!