%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Datei:        bsp_inout.m
%
% Beschreibung: Vorlesungsbeispiele zur FHA-Vorlesung 
%               MATLAB Eingabe und Ausgabe
%
% Datum:        18.03.07
% Autor:        Ulrich Wohlfarth
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

echo off
pause off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Steuerung der Bildschirmausgabe

echo mittelwerte on
[A, G] = mittelwerte (1:2:7)
echo mittelwerte off

for i=1:2:6, disp(i), pause(i), end, disp('Ende'), pause

pause

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Benutzerdialoge

text = ['Das ist', ' ', 'ein Text!']
whos text

pause

preis = input (['Wieviel kostet heuer \n', ...
                'die Wies''n-Maß ?      ']);
waehrung = input ('Währung ?             ', 's');

disp (['Aber ', num2str(preis, '%0.2f'), ' ', waehrung, ...
       ' pro Maß wird ein teurer Rausch!']);
disp (' ');                                                  % Leerzeile
ausgabe = sprintf ('Zwei Maß kosten dann %2.2f %s.', ...
                    preis*2, waehrung);
disp (ausgabe);
mehr = sprintf ('%4d Maß kosten dann %2.2f.\n', ...
                [3:5; (3:5)*preis]);
disp (mehr);

pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Import und Export von Daten
% Die folgenden Zeilen benötigen Schreibrecht im aktuellen Verzeichnis
% andernfalls bitte auskommentieren

test_vektor = [0:0.1:10]';                       % Spaltenvektor
test_matrix = [test_vektor cos(test_vektor)];    % Matrix

pause
save test                          % Speichern in Datei test.mat
clear                              % Workspace löschen
load test                          % Laden aus Datei test.mat
who                                % Anzeige des Workspace

pause
save test.txt -ascii test_matrix   % Speichern in Datei test.txt
clear                              % Workspace löschen
load test.txt                      % Laden aus Datei test.txt
who                                % Anzeige des Workspace

pause

preis = 6.50;
bier_id = fopen ('bier.txt', 'w');
fprintf (bier_id, '%4d Maß kosten dann %2.2f.\r\n', ...
                    [3:5; (3:5)*preis]);
fclose (bier_id);

pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Betriebssystemaufruf und Dateiverwaltung

mkdir ('testverzeichnis')
verzeichnis = dir ('testverzeich*');
eval (['!rmdir ', verzeichnis.name])

pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Graphische Darstellung

figure
gcf

get(gcf)
get(gcf, 'Position')
set(gcf, 'Position', [82 547 500 400])

pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Zweidimensionale Graphiken

figure;
subplot (121);
    plot ([-5:0.1:5], cos ((-5:0.1:5)*pi), 'k:');
    hold on;
    fplot ('2*sin(pi*x)/pi/x', [-5 5], 'k--');
    fplot (@(x)2.*sin(pi.*x)./pi./x,[-5 5], 'k--')
subplot (122);
    t = (0:20)*0.9*pi;
    plot (cos (t), sin (t));

pause
figure;
subplot (121);
    ezplot ('x^2 - y^2 -2');
subplot (122);
    ezplot ('sin(3*t) * cos(t) / (t+pi)', 'sin(3*t) * sin(t) / (t+pi)', [0,4*pi]);
%print -f2 -depsc 'plot_ezplot';

pause
% Berechnung des Frequenzgangs eines PT1-Glieds der Zeitkonstante T
% im Laplace-Bereich: 1/(1+sT) mit s = j*omega

T              = 0.04;                    % Zeitkonstante PT_1 [s]
omega          = logspace (0, 3, 100);    % Frequenzvektor [rad/s]
frequenzgang   = (T*j*omega + 1).^(-1);   % Frequenzgang PT_1
amplitudengang = abs (frequenzgang);      % Amplitudengang PT_1
phasengang     = angle (frequenzgang);    % Phasengang PT_1

figure (3);                               % Erzeugt Figure 3
clf;                                      % Löscht ggf. alte Plots
subplot (211);                            % Subplot: Amplitude
    loglog (omega, amplitudengang, 'b-');         % PT_1
    hold on;
    loglog (omega, amplitudengang.^(-1), 'r--');  % PD: invers
    title ('Übertragungsfunktionen');
    xlabel ('\omega [rad s^-^1]');
    ylabel ('Amplitude [dB]');
    legend ('PT_1-Glied', 'PD-Glied');
    axis ([1 1e3 0.01 100]);              % Sichtbarer Bereich
    skala = -40:20:40;                    % Gewünschte Achsteilung
    set (gca, 'ytick', 10.^(skala/20));   % Setzt Achsteilung (dB)
    set (gca, 'yticklabel', skala);       % Setzt Beschriftung

subplot (212);                            % Subplot: Phase
    semilogx (omega, phasengang*180/pi, 'b-');    % PT_1
    hold on;
    semilogx (omega, -phasengang*180/pi, 'r--');  % PD: invers
    grid on;                              % Aktiviert Gitterlinien
    xlabel ('\omega [rad s^-^1]');
    ylabel ('Phase [Grad]');
    axis ([1 1e3 -90 90]);                % Sichtbarer Bereich
    skala = -90:30:90;                    % Gewünschte Achsteilung
    set (gca, 'ytick', skala);            % Setzt Achsteilung (Grad)
    set (gca, 'yticklabel', skala);       % Setzt Beschriftung
%print -f3 -deps plot_frequ;             % Ausgabe als EPS-Datei

pause
figure(4)
pt1 = tf ([1], [0.04 1]);
pd  = tf ([0.04 1], [1]);
bode (pt1, 'b-', pd, 'r--')
%print -f4 -depsc 'plot_frequ_tf';            % Ausgabe als EPS-Datei

pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dreidimensionale Graphiken (3D-Graphik)

figure(5)
phi = (0:100) / 100 * 2*pi;
plot3 (sin (2*phi), cos (3*phi), phi, 'b*');
xlabel('x=sin(2*phi)')
ylabel('y=cos(3*phi)')
zlabel('z=phi')
%print -f5 -depsc plot_3dsincos

pause
% Beispiel
x = 0:0.05:2;
y = -1:0.2:1;

[X, Y] = meshgrid (x, y);       % Erzeugt Matrizen über Bereich von x, y
Z = (Y+1) .* cos (2*pi*X.^2) + (Y-1) .* sin (2*pi*X.^2) / 5;

pause

figure 
subplot (221);
    surf (X, Y, Z);
    view (-40, 30);
    title ('surf (X, Y, Z); view (-40, 30);');
pause
subplot (222);
    mesh (X, Y, Z);
    view (-20, 30);
    title ('mesh (X, Y, Z); view (-20, 30);');
    xlabel ('grid off; box on;');
    grid off;
    box on;
pause
subplot (223);
    waterfall (X, Y, Z);
    view (-20, 10);
    title ('waterfall (X, Y, Z); view (-20, 10);');
pause
subplot (224);
    contour (X, Y, Z);
    title ('contour (X, Y, Z);');
%print -f6 -deps plot_3d;
   
pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Importieren, Exportieren und Drucken von Graphiken

%print -f1;              % Drucken von Figure 1 auf Standarddrucker
print -f1 -dmeta bild;  % Speichern als Windows-Metafile (bild.emf)
print -f1 -depsc bild;  % Speichern als Farb-Postscript  (bild.eps)

% Bilder Einbinden

figure
A = imread ('tee.jpg', 'jpg');
image (A)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
