summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/numericplots/MatlabFunctions/dspace2struct.m
blob: f1195fc933f0337119b5aeaf2861e9ac6344cfa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function dat = dspace2struct(filename,version,alternative_struct_name)
% ------------------------------------------------
% dat = dspace2struct(dspacename,version)
% Function to export dSPACE-mat-file to Matlab struct.
% The fieldnames of the struct are the same as the dSPACE-signalnames.
% ------------------------------------------------
% Output:
% dat        ... matlab struct
%
% Input:
% filename   ... (optional) .mat-File generated by dSPACE (string) (
% version    ... (optional) version = 'NG' if using dSpace Next Genertion
% alternative_struct_name ...(optional) needed if structname is different
%                             from filename. Often caused by renaming .mat
%                             files after saving.
%
% author: Alexander Michel
% date: 2010/08/09
%
% changes: - 2011/06/29 Alexander Michel: - customized for dSpace Next
%                                           Generation data structure
%          - 2011/07/29 Alexander Michel: - GUI added when no file is
%                                           provided
%                                         - Path can be provided
%          - 2012/05/02 Alexander Michel: - Bug fixing for dSpace NG naming
%                                           of calculations
%                                         - Multiple XData for dSpace NG
%                                           added
%          - 2013/06/17 Alexander Michel: - Array out for dSpace NG support
%                                           added
%          - 2013/07/11 Alexander Michel: - alternative structname support
%
% Copyright 2012 Thomas König, Alexander Michel
%
% This file is part of NumericPlots.
%
% NumericPlots is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% any later version.
%
% NumericPlots is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with NumericPlots.  If not, see <http://www.gnu.org/licenses/>.

if(nargin<1 || strcmp(filename,''))
    [fname datapath] = uigetfile('*.mat','choose .mat-file generated by dSPACE');
    filename = fullfile(datapath, fname);
    if isequal(fname,0) || isequal(datapath,0)
        disp('User pressed cancel')
        return;
    else
        disp(['User selected ', filename])
    end
end

if(isempty(strfind(filename,'\')))
    idx1name = 1;
else
    idxes = strfind(filename,'\');
    idx1name = idxes(end)+1;
end

if(nargin>2)
    dspacename = alternative_struct_name;
else
    if(isempty(strfind(filename,'.mat')))
        dspacename = filename(idx1name:end);
    else
        dspacename = filename(idx1name:end-4);
    end
end


if(nargin<2)
    version = 'OG';
end

load(filename);

dspace = eval(genvarname(dspacename));
if(strcmp(version,'NG'))
    for ii = 1:length(dspace.X)
        dat.time{ii} = dspace.X(ii).Data;
    end
else
    dat.time = dspace.X.Data;
end
validx = [];
for ii = 1:length(dspace.Y)
    if(strcmp(version,'NG'))
        if(isempty(dspace.Y(ii).Path))
            newfieldname = dspace.Y(ii).Name;
        else
            stridx = strfind(dspace.Y(ii).Path,'/');
            newfieldname = dspace.Y(ii).Path(stridx(end)+1:end);
        end
    else
        stridx = strfind(dspace.Y(ii).Name,'"');
        lastname = dspace.Y(ii).Name(stridx(end-1)+1:stridx(end)-1);
        stridxM1 = strfind(lastname,'[');
        if(~isempty(stridxM1))
            stridxM2 = strfind(lastname,']');
            if(~isempty(stridxM2))
                validx = sscanf(lastname(stridxM1:stridxM2),'[%i,%i]');
                possiblefieldname = lastname(1:stridxM1-1);
            end
        else
            possiblefieldname = lastname;
        end
        if(isempty(strfind(possiblefieldname,'In1')) && isempty(strfind(possiblefieldname,'Out1')))
            newfieldname = possiblefieldname;
        else
            newfieldname = dspace.Y(ii).Name(stridx(end-3)+1:stridx(end-2)-1);
        end
    end
    if(isvarname(newfieldname) == 0)
        newfieldname = newfieldname(isstrprop(newfieldname,'alphanum'));
        if(isvarname(newfieldname) == 0)
            newfieldname = newfieldname(2:end);
        end
    end
    if(strcmp(version,'NG'))
        if(isfield(dat,newfieldname))
            dat.(newfieldname).Data = [dat.(newfieldname).Data; dspace.Y(ii).Data];
        else
            dat.(newfieldname).Data =  dspace.Y(ii).Data;
        end
        dat.(newfieldname).timeidx = dspace.Y(ii).XIndex;
    else
        if(isempty(validx))
            dat.(newfieldname) = dspace.Y(ii).Data;
        else
            dat.(newfieldname){validx(2)} = dspace.Y(ii).Data;
            validx = [];
        end
    end
end
if(~strcmp(version,'NG'));
    dat.dSPACESettings.Capture = dspace.Capture;
    dat.dSPACESettings.Description = dspace.Description;
    dat.dSPACESettings.RTProgram = dspace.RTProgram;
end