Commit 2e6fa975 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Initial checkin of the matlab library for the control and faultdiagnosis of...

Initial checkin of the matlab library for the control and faultdiagnosis of infinite dimensional systems
parents
classdef (Abstract) Data < handle & matlab.mixin.Heterogeneous
%EXPORTDATA Summary of this class goes here
% Detailed explanation goes here
properties
basepath = '.';
foldername = '.';
filename = 'data';
N = 201;
end
methods
function obj = Data(varargin)
for arg=1:2:length(varargin)
obj.(varargin{arg}) = varargin{arg + 1};
end
end
function p = path(obj, k)
if ~exist('k', 'var')
k = '';
end
p = [obj.absolutepath filesep obj.addExtension(k)];
end
function p = absolutepath(obj)
p = [obj.basepath filesep obj.foldername];
end
function f = addExtension(obj, k)
if ~exist('k', 'var')
name = obj.filename;
elseif ischar(k)
name = [obj.filename k];
elseif isnumeric(k)
name = [obj.filename num2str(k)];
end
if isempty(regexpi(obj.filename, '\w*\.(dat)$'))
f = [name '.dat'];
else
error('Dateiname muss ohne Endung übergebenw erden!');
end
end
end
methods(Sealed)
function export(obj)
for k = 1:length(obj)
if ~obj(k).isempty
if ~isdir(obj(k).absolutepath)
mkdir(obj(k).absolutepath);
end
obj(k).innerexport();
fprintf(['export: %s \n'], obj(k).path);
else
warning(['Object ' num2str(k) ' is empty']);
end
end
end
end
methods (Abstract, Access = protected)
innerexport(obj);
end
methods (Static)
function exportAll( exp_struct)
structfun(@(x)export(x), exp_struct);
end
end
end
function [ x_,y_,z ] = data2dat( filename, x, y, z, N, xRange, yRange )
%DATA2DAT Converts the given data to ".dat" format to use it with Latex
%tikzplot package.
% Inputparameters:
%
%data2dat( filename, x, y)
% filename: filename for the new ".dat" file
% x: x-coordinate of the data as a vactor
% y: y-coordinate of the data as a vactor
%
%data2dat( filename, x, y, z)
% filename: filename for the new ".dat" file
% x: x-coordinate of the data as a matrix
% y: y-coordinate of the data as a matrix
% z: z-coordinate of the data as a matrix
%
%data2dat( filename, x, y, z, xRange)
% filename: filename for the new ".dat" file
% x: x-coordinate of the data as a vactor
% y: y-coordinate of the data as a vactor
% z: has to be empty ~
% xRange: export.range of x-coordinate as vector with [x_min x_max]
%
%data2dat( filename, x, y, z, xRange, yRange )
% filename: filename for the new ".dat" file
% x: x-coordinate of the data as a matrix
% y: y-coordinate of the data as a matrix
% z: z-coordinate of the data as a matrix
% xRange: export.range of x-coordinate as vector with [x_min x_max]
% yRange: export.range of x-coordinate as vector with [y_min y_max]
if ~exist('xRange', 'var')
xRange(1)=x(1);
xRange(2)=x(end);
end
if ~exist('yRange', 'var')
yRange(1)=y(1);
yRange(2)=y(end);
end
if ~exist('N', 'var')
N = 60;
end
if exist('z','var')
z = squeeze(z);
x_=linspace(xRange(1), xRange(2), N);
y_=linspace(yRange(1), yRange(2), N);
[x_,y_]=meshgrid(x_,y_);
[x,y]=meshgrid(x,y);
z=interp2(x,y,z',x_,y_);
out=[x_(:),y_(:),z(:)];
else
x_=linspace(xRange(1),xRange(2), length(x), N)';
%Vektoren in das richtige Format f�r interp1 bringen:
y=MakeColumnVectors(y);
x=MakeColumnVectors(x);
y_=interp1(x,y,x_);
out=[x_, y_];
end
if isempty(regexpi(filename,'\w*\.(dat)$'))
filename=[filename '.dat'];
end
save(filename, 'out', '-ascii', '-tabs')
classdef dd < export.Data
%DD Export of two-dimensional dataset
% Exports the Matrix 'M' with the header-row 'header' to draw plots
% with pgfplots.
%
% data = export.dd(...
% 'M', datamatrix, ...
% 'header', {'column1', 'column2', ... }, ...
% 'foldername', 'path to directory', ...
% 'filename', 'export filename', ...
% 'N', number of discretization points for the
% export.
%
% data.export(): Export the data to <foldername>/<filename>.dat :
%
% column1 column2 ...
% . . .
% . . .
% . . .
%
%
% Pgfplots example:
% \begin{tikzpicture}
% \begin{axis}
% \addplot table[x = column1 , y = column2]{<foldername>/<filename>.dat};
% \addplot table[x = column1 , y = ... ]{<foldername>/<filename>.dat};
% \end{axis};
% \end{tikzpicture}
properties
M = [];
header = {};
plotfnc = @plot;
splitN = inf;
jumps = [];
end
methods
function obj = dd(varargin)
obj@export.Data();
for arg=1:2:length(varargin)
obj.(varargin{arg}) = varargin{arg + 1};
end
end
function set.M(obj, M)
if iscell(M)
M = cellfun(@(c) misc.mcv(c), M);
end
obj.M = M;
end
function o = out(obj)
if obj.N > 0 && obj.N < size(obj.M, 1)
x = linspace(obj.M(1,1), obj.M(end,1), obj.N)';
o = interp1(obj.M(:,1), obj.M(:,2:end), x);
o = [x, o];
else
o = obj.M;
end
if ~isempty(obj.jumps)
for k = 1:numel(obj.jumps)
J = obj.jumps{k};
idxJump = find(o(:,J.idx_t) >= J.tj, 1);
oBefore = o(1:(idxJump-1), :);
oAfter = o((idxJump):end, :);
o = [oBefore; ...
oAfter(1,:); ...
oAfter];
o(idxJump, J.idx_j) = oBefore(end, J.idx_j);
end
end
end
function is = isempty(obj)
is = isempty(obj.header) || isempty(obj.M);
end
function plot(obj, idx)
if nargin == 1
obj.plotfnc(obj.M(:, 1), obj.M(:, 2:end));
legend(obj.header{2:end});
else
obj.plotfnc(obj.M(:, 1), obj.M(:, idx));
legend(obj.header{idx});
end
end
end
methods (Access = protected)
function innerexport(obj)
o = obj.out;
if obj.N > obj.splitN
idx = 1;
splits = ceil(obj.N / obj.splitN);
for k = 1:splits
if k < splits
idx = idx(end):(idx(end) + obj.splitN);
else
idx = idx(end):obj.N;
end
dlmwrite(obj.path(k), sprintf('%s\t', obj.header{:}), 'delimiter', '')
dlmwrite(obj.path(k), o(idx,:), '-append', 'delimiter', '\t')
end
else
dlmwrite(obj.path, sprintf('%s\t', obj.header{:}), 'delimiter', '')
dlmwrite(obj.path, o, '-append', 'delimiter', '\t')
end
end
end
end
classdef ddd < export.Data
%DDD export 3D data
%
properties
x;
y;
z;
xRange;
yRange;
end
properties(Dependent)
out;
end
methods
function obj = ddd(varargin)
obj@export.Data('N', 60);
for arg=1:2:length(varargin)
obj.(varargin{arg}) = varargin{arg + 1};
end
end
function xR = get.xRange(obj)
if isempty(obj.xRange)
xR = [obj.x(1), obj.x(end)];
else
xR = obj.xRange;
end
end
function yR = get.yRange(obj)
if isempty(obj.yRange)
yR = [obj.y(1), obj.y(end)];
else
yR = obj.yRange;
end
end
function set.z(obj, z)
obj.z = squeeze(z);
end
function out = get.out(obj)
o.x = linspace(obj.xRange(1), obj.xRange(2), 60);
o.y = linspace(obj.yRange(1), obj.yRange(2), 60);
[o.x, o.y] = meshgrid(o.x, o.y);
[grid.x, grid.y] = meshgrid(obj.x, obj.y);
o.z = interp2(grid.x, grid.y, obj.z', o.x, o.y);
out = [o.x(:), o.y(:), o.z(:)];
end
function i = isempty(obj)
i = isempty(obj.x) || isempty(obj.y) || isempty(obj.z);
end
end
methods( Access = protected)
function innerexport(obj)
o = obj.out;
save(obj.path, 'o', '-ascii', '-tabs')
end
end
end
%% PLOT
t = (0:0.2:10)'; % Darstellung als Spaltenvektor ist wichtig!
y1 = sin(t);
y2 = cos(t);
basepath = [pwd filesep '+export'];
data.plot = export.dd(...
'M', [t, y1, y2], ...
'header', {'t', 'y1', 'y2'}, ...
'filename', 'plot', ...
'basepath', basepath ...
);
%% SURF
z = (0:20)';
[X, Y] = meshgrid(t , z);
w = sin(X) + cos(Y);
surf(X,Y,w)
data.surf = export.ddd(...
'x', z, ...
'y', t, ...
'z', w, ...
'filename', 'surf', ...
'basepath', basepath ...
);
structfun(@(x)export(x), data)
disp(['call "pdflatex example.tex" in the path "' basepath '" to build the pdf file']);
\ No newline at end of file
\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ylabel={$y_i(t)$},
xlabel={$t$},
]
\addplot [black] table[x=t, y = y1]{plot.dat};
\addplot [red] table[x=t, y = y2]{plot.dat};
\end{axis};
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
xlabel={$z$},
ylabel={$t$},
zlabel={$w(z,t)$},
]
\addplot3[surf,mesh/ordering=y varies, mesh/rows=60] table {surf.dat};
\end{axis}
\end{tikzpicture}
\end{document}
\ No newline at end of file
function [ files ] = exportCode( function_name, exportPath, excludePath )
%EXPORTFUNCTION !experimental!
% [ files ] = exportCode( function_name, exportPath, excludePath )
% Uses the matlab.codetools.requiredFilesAndProducts to find the
% functions, the matlab function "function_name" needs to run and copies
% all of these functions to the path "exportPath". If special paths
% should not be considered, they can be given in "excludePath". As result
% "files" a list with all the copied files, i.e., matlab functions, is
% returned.
%
% #TODO example
rmpath(excludePath)
files = matlab.codetools.requiredFilesAndProducts(function_name);
if ~exist('exportPath', 'var')
exportPath = function_name;
end
if ~isdir(exportPath)
mkdir(exportPath)
else
userinp = questdlg(['Der Pfad ' exportPath ' ist bereits vorhanden. Sollen die Dateien ?berschrieben werden?']);
if ~strcmp(userinp, 'Yes')
return
end
%TODO: Falls im Ordner noch dateien eliegen, dann müssen diese gelöscht
%werden!
end
for k = 1:length(files)
fid=fopen(files{k});
tline = fgetl(fid);
while ischar(tline)
if ~isempty(strfind(tline, 'addpath'))
warning(['Die Datei ' files{k} ' enthält einen addpath Befehl!'])
end
tline = fgetl(fid);
end
fclose(fid);
copyfile(files{k}, exportPath, 'f');
end
addpath(excludePath)
end
function [ ] = matrix2dat( filename, M, columnheads, N_plot)
%MATRIX2DAT Converts the given data to ".dat" format to use it with Latex
%tikzplot package.
%
%function [ ] = matrix2dat( filename, M, columnheads, N_plot)
% filename: filename for the new ".dat" file
% M: The data to be export.d either as Matrix or Cell Array.
% columnHeads: The columndescriptions as cell array of string
% N_plot[optional, default=200]: Number of supportingpoints the export.
% data should have
if isempty(regexpi(filename,'\w*\.(dat)$'))
filename=[filename '.dat'];
end
if iscell(M)
M_=[];
for k=1:length(M)
M_ = [M_ misc.mcv(M{k})];
end
M=M_;
end
if ~exist('N_plot', 'var')
N_plot=200;
end
if size(M,1) > N_plot && N_plot > 0
x=linspace(M(1,1), M(end,1), N_plot)';
M=interp1(M(:,1), M(:,2:end), x);
M=[x, M];
end
dlmwrite(filename, sprintf('%s\t',columnheads{:}), 'delimiter', '')
dlmwrite(filename, M, '-append', 'delimiter', '\t')
\ No newline at end of file
classdef namevalues < export.Data
%VARS Summary of this class goes here
% Detailed explanation goes here
%
% example:
% --------------------------------------
% data = export.namevalues('vars', {'key1', 'key2', 'key3'}, 'values',
% [1, 2, 3]);
% keys.export();
properties
vars;
values;
seperator = '=';
end
properties (Dependent)
Tab;
end
methods
function obj = namevalues(varargin)
obj@export.Data();
for arg=1:2:length(varargin)
obj.(varargin{arg}) = varargin{arg + 1};
end
end
function T = get.Tab(obj)
for k = 1:length(obj.vars)
if length(obj.vars{k}) == 1
obj.vars{k} = ['"' obj.vars{k} '"'];
end
end
seps = repmat({obj.seperator}, size(obj.vars));
T = table(obj.vars(:), seps(:), obj.values(:));
end
function is = isempty(obj)
is = isempty(obj.vars) || isempty(obj.values);
end
function addnamevalue(obj, name, value)
obj.vars{end+1} = name;
obj.values(end+1) = value;
end
end
methods ( Access = protected)
function innerexport(obj)
writetable(obj.Tab, obj.path, 'Delimiter', 'space', 'WriteVariableNames', false);
end
end
end
t y1 y2
0 0 1
0.2 0.19867 0.98007
0.4 0.38942 0.92106
0.6 0.56464 0.82534
0.8 0.71736 0.69671
1 0.84147 0.5403
1.2 0.93204 0.36236
1.4 0.98545 0.16997
1.6 0.99957 -0.0292
1.8 0.97385 -0.2272
2 0.9093 -0.41615
2.2 0.8085 -0.5885
2.4 0.67546 -0.73739
2.6 0.5155 -0.85689
2.8 0.33499 -0.94222
3 0.14112 -0.98999
3.2 -0.058374 -0.99829
3.4 -0.25554 -0.9668
3.6 -0.44252 -0.89676
3.8 -0.61186 -0.79097
4 -0.7568 -0.65364
4.2 -0.87158 -0.49026
4.4 -0.9516 -0.30733
4.6 -0.99369 -0.11215
4.8 -0.99616 0.087499
5 -0.95892 0.28366
5.2 -0.88345 0.46852
5.4 -0.77276 0.63469
5.6 -0.63127 0.77557
5.8 -0.4646 0.88552
6 -0.27942 0.96017
6.2 -0.083089 0.99654
6.4 0.11655 0.99318
6.6 0.31154 0.95023
6.8 0.49411 0.8694
7 0.65699 0.7539
7.2 0.79367 0.60835
7.4 0.89871 0.43855
7.6 0.96792 0.25126
7.8 0.99854 0.053955
8 0.98936 -0.1455
8.2 0.94073 -0.33915
8.4 0.8546 -0.51929