How can i create a subplot in matlab?

49 views (last 30 days)
Hi,
How can i create a subplot in matlab with my diagram? (2 Columns and 2 Rows) -> See picture:
Here is my code:
%deploytool
%Datei heisst "Testbeispiel_Harald"
%Der Readtable muss im in D:\work\matlab -> Verzeichnis sein
clc % löscht den Bildschirm
clear all % löscht alle Variablen
%*************************************************************************
%Hier wird die Matrix eingelesen.
%Klassenbreite - Anzahl Klassen - Startpunkt
%2 6 10
%2 8 0
%Jetzt kommt das wirkliche Daten
%x-Wert - y-Wert - Häufigkeit
%12 0 10
%14 0 30
%16 0 54
%18 0 51
%20 0 35
%22 0 12
%14 2 25
%16 2 35
%*************************************************************************
%Als Input wünsche ich mir den linken unteren Punkt!!!!
%Einlesen der Daten
%Input_Matrix = textread('Testbeispiel_RainflowX.txt')
Input_Matrix=[2 6 10 ;2 8 0;12 0 10;14 0 30;16 0 54;18 0 51;20 0 35;22 0 12;14 2 25;16 2 35]
%Bestimmung der Größe der Matrix die als .txt eingelesen wird
[zeilen,spalten]=size(Input_Matrix)%analysiert die grösse der eingelesenen txt datei
%***********Auslesen spezifischer Daten der Matrix"*********************************
Haeufigkeit=Input_Matrix(3:zeilen,3)%(:,3)%gesamte dritte Spalte (exkl. die ersten 2 Zeilen)
ycord=Input_Matrix(3:zeilen,2)%gesamte x-Koordianten (exkl. die ersten 2 Zeilen)
xcord=Input_Matrix(3:zeilen,1)%gesamte y-Koordinaten (exkl. die ersten 2 Zeilen)
x_Klassenbreite=Input_Matrix(1,1) %Wie breit ist die Klasse in x
y_Klassenbreite=Input_Matrix(2,1) %Wie breit ist die Klasse in y
x_Klassen=Input_Matrix(1,2) %Klassenanzahl x?
y_Klassen=Input_Matrix(2,2) %Klassenanzahl y?
x_Start=Input_Matrix(1,3) %KS Startwert in x?
y_Start=Input_Matrix(2,3) %KS Startwert in y?
%************Berechnungen für die Achsendarstellungen**********************
x_Achse_Start=x_Start; %Startpunkt auf x-Achse
y_Achse_Start=y_Start; %Startpunkt auf x-Achse
x_Achse_Ende= x_Klassen * x_Klassenbreite+x_Achse_Start %EndWert auf der x-Achse
y_Achse_Ende= y_Klassen * y_Klassenbreite+y_Achse_Start %%EndWert auf der y-Achse
n=10; % höchst zulässige Teilung
if x_Klassen > n
x_Teilung = n;
else
x_Teilung = x_Klassen;
end
if y_Klassen > n
y_Teilung = n;
else
y_Teilung = y_Klassen;
end
%x_Achse_thicks = (x_Achse_Ende-x_Achse_Start)/10 %x-Achse Teilung
%y_Achse_thicks = (y_Achse_Ende)/10 %x-Achse Teilung
x_Achse_thicks1 = round(linspace(x_Achse_Start,x_Achse_Ende,x_Teilung))
y_Achse_thicks1 = round(linspace(y_Achse_Start,y_Achse_Ende,y_Teilung))
%figure;
figure1 = figure('Color',[1 0.968627452850342 0.921568632125854]);
%Punkte_Matrix => Matrix deren Inhalt die Koordinaten aller Eckpunkte der Rechtecke hat
%Größe => (x Zeilen/ 2 Spalten)
%Benötigte Größe der Punkte_Matrix definieren bzw. alle Werte mit Null initalisieren:
Anzahl_Zeilen_Punkte_Matrix=(zeilen-2)*4;
Anzahl_Spalten_Punkte_Matrix=2;%Fixwert 2 Spalten
Punkte_Matrix = zeros(Anzahl_Zeilen_Punkte_Matrix,Anzahl_Spalten_Punkte_Matrix);
Connections = reshape(1:Anzahl_Zeilen_Punkte_Matrix,4,[])';%Erzeugt eine Matrix für die
%korrekte Verbindung der Rechtecks-Punkte
value=length(ycord);
%Punkte_Matrix mit den Startwerten initalisieren
%X Werte
x=1;
for i = 1:value
Punkte_Matrix(x,1)=xcord(i);
x=x+4
end
%Punkte_Matrix mit den Startwerten initalisieren
%y Werte
x=1;
for i = 1:value
Punkte_Matrix(x,2)=ycord(i);
x=x+4
end
%Punkte_Matrix mit Punkt2 befüllen
%y Werte
x=2;
for i = 1:value
Punkte_Matrix(x,1)=xcord(i)+x_Klassenbreite;
x=x+4
end
x=2;
for i = 1:value
Punkte_Matrix(x,2)=ycord(i);
x=x+4
end
%Punkte_Matrix mit Punkt3 befüllen
%y Werte
x=3;
for i = 1:value
Punkte_Matrix(x,1)=xcord(i)+x_Klassenbreite;
x=x+4
end
x=3;
for i = 1:value
Punkte_Matrix(x,2)=ycord(i)+y_Klassenbreite;
x=x+4
end
%Punkte_Matrix mit Punkt4befüllen
%y Werte
x=4;
for i = 1:value
Punkte_Matrix(x,1)=xcord(i);
x=x+4
end
x=4;
for i = 1:value
Punkte_Matrix(x,2)=ycord(i)+y_Klassenbreite;
x=x+4
end
patch('Faces',Connections,'Vertices',Punkte_Matrix,'FaceVertexCData',Haeufigkeit,'FaceColor','flat','EdgeColor','none') %Befehl fürs "zeichnen"
colormap(jet)
%figure1 = figure('Color',[1 0.968627452850342 0.921568632125854]);
label_colorbar=colorbar
ylabel(label_colorbar,'cycles')
[maxValue, rowIdx] = max(Haeufigkeit(:,1),[],1)
caxis([0,maxValue]) % setzt die color limits
title('Harald will keine Überschrift Platzhalter','FontSize',14);% Create title
xlabel('Mean Value [MPa]','FontWeight','bold');% Create label
ylabel('Amplitude [MPa]','FontWeight','bold');% Create label
legend('Legende brauchen wir auch keine');
axis([x_Achse_Start x_Achse_Ende 0 y_Achse_Ende])
grid off
set(gca,'XTick',x_Achse_thicks1)
set(gca,'YTick',y_Achse_thicks1)
set(gca,'XGrid','off','YGrid','off','ZGrid','off'); %Raster abschalten
matlab2tikz('Testbeispiel_Harald.tikz', 'height', '\figureheight', 'width', '\figurewidth');
%matlab2tikz('Testbeispiel_Harald.tikz', 'height', '\figureheight', 'width', '\figurewidth');
%Test3=matlab2tikz('Testbeispiel_Harald.tex');
%gca.XTickLabelRotation = 45; % bei bedarf
%gca.YTickLabelRotation = 45; % bei bedarf
%set(gca,'YTickLabel',{''})
%set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})
%set(gca, 'XTickMode', 'manual', 'XTick', Ticks, 'xlim', [0,20]);
It does not matter if my diagram -> Plot1 - Plot6 is always the same. Should is use a function for it? A loop? I can solve this problem easily in latex but not in matlab cause I am a beginner. thank you in advance.

Accepted Answer

Star Strider
Star Strider on 19 Sep 2016
This works:
figure(1)
subplot(3,2,1)
plot([1:0.1:5], sin([1:0.1:5]*1*pi/5))
text(2, 0.5, 'Plot 1')
grid
subplot(3,2,2)
plot([1:0.1:5], sin([1:0.1:5]*2*pi/5))
text(2, 0.5, 'Plot 2')
grid
subplot(3,2,3)
plot([1:0.1:5], sin([1:0.1:5]*3*pi/5))
text(2, 0.5, 'Plot 3')
grid
subplot(3,2,4)
plot([1:0.1:5], sin([1:0.1:5]*4*pi/5))
text(2, 0.5, 'Plot 4')
grid
subplot(3,2,5)
plot([1:0.1:5], sin([1:0.1:5]*5*pi/5))
text(2, 0.5, 'Plot 5')
grid
subplot(3,2,6)
plot([1:0.1:5], sin([1:0.1:5]*6*pi/5))
text(2, 0.5, 'Plot 6')
grid
You can also plot them in a loop. It works like any other for loop:
figure(1)
for k1 = 1:6
subplot(3,2,k1)
plot([1:0.1:5], sin([1:0.1:5]*k1*pi/5))
text(2, 0.5, sprintf('Plot %d',k1))
grid
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!