summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/numericplots/MatlabFunctions/struct2latex.m
blob: 8edb5d543a3ce037251797d573ff22cb93add33e (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function num = struct2latex(data,xname,downsample,filtertype,filename,postfix,options)
%------------------------------------------------
%2010/09/14 version 1.0
%num = struct2latex(data,xname,downsample,filename,postfix,options)
%Function to export a matlabstruct to a tex-file for using NumericPlots. 
%xname, downsample, filename, postfix and options are optional parameters.
%struct2latex is based on the function export2latex.
%------------------------------------------------
%output:
%num        ...number of y-data exported
%input:
%data       ...datastruct
%xname      ...name of the x-Axis
%downsample ...downsampling factor (optional, default = 1)
%filtertype ...using filtering data {'none','MinMax','Hull'}(optional, default = 'none')
%filename   ...name of the tex-file (optional, default io)
%postfix    ...postfix for fieldnames in the tex-file (optional, default '')
%options    ...options for export2latex (optional)
%

% author: Alexander Michel
% date: 2010/08/09
% changed by Alexander Michel: 
%    2010/09/14 mapping of column-vectors 
%    2010/09/14 postfix option added
%    2012/08/03 filters added
%    2013/06/26 data consistency check added
%    2013/08/09 data consistency check for x-vector dimension added
%               bugfixing of mapping of column-vectors
%               bugfixing of fnamesExcludelist
%
% Copyright 2010 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/>.


%choose x-Axis
allfnames = fieldnames(data);
if(nargin<2)    
    [xnamenum,selected] = listdlg('PromptString','Choose x-Axis:','SelectionMode','single','ListString',allfnames(1:end));
    if (selected==0)
        error('No x-Axis chosen!');
    end
    xname = allfnames{xnamenum};
end

%exclude special fields from export
fnamesExcludelist = {xname,'dSPACESettings'};
kk = 1;
for ii = 1:length(allfnames)
    if(~strcmp(allfnames{ii},fnamesExcludelist))
        fnames{kk} = allfnames{ii};
        kk = kk+1;
    end
end


% check x data consistency and
% convert to column vectors if necessary
[nor,noc] = size(data.(xname));
if(nor>1)
     if(noc>1)
         error('struct2latex supports only vectors as x-vectors');
     else
         data.(xname) = data.(xname)';
     end
end  

% check y data consistency and
% convert to column vectors if necessary
for ii = 1:length(fnames)
    [nor,noc] = size(data.(fnames{ii}));
    if(nor>1)
         if(noc>1)
             error('struct2latex supports only vectors as y-vectors');
         else
             data.(fnames{ii}) = data.(fnames{ii})';
         end
    end  
    if(length(data.(xname))~=length(data.(fnames{ii})))
        error(['Error in ' fnames{ii} '! Only vectors of the same length are supported!']);
    end 
end

%downsample
if(nargin<3)
    downsample = 1;
end
idx = 1:downsample:length(data.(xname));

%downsample
if(nargin<4)
    filtertype = 'none';
end


%postfix
if(nargin<6)
    postfix = '';
end

if(length(idx)>5000)
    buttonname = questdlg('Warning! Number of points exceeds 5000! It is recommended to increase the downsampling factor!','Increase the downsampling factor?','Yes','No','Yes');
    switch buttonname
        case 'Yes'
            dlgoptions.Resize='on';
            dlgoptions.WindowStyle='normal';
            dlgoptions.Interpreter='tex';
            defans{1} = num2str(ceil(length(data.(xname))/5000));
            answer = inputdlg('Downsampling factor','Input Downsampling factor',1,defans,dlgoptions);
            downsample = str2num(answer{1});
            idx = 1:downsample:length(data.(xname));
        case 'No'
            warning('number of points exceeds 5000');
    end
end


% get x-data
xbuffer = data.(xname);
[nor,noc] = size(xbuffer);
if(nor>1)
     if(noc>1)
         error('struct2latex supports only vectors');
     else
         xbuffer = xbuffer';
     end
end

% get y-data
latexfnames = genlatexnames(fnames);
for ii = 1:length(fnames)   
    ybuffer = data.(fnames{ii});      
    switch(filtertype)
        case{'none'}
            ydata = ybuffer(idx);
            xdata = xbuffer(idx);
        case{'MinMax'}
            [xdata,ydata] = LatexFilterMinMax(xbuffer,ybuffer,floor(length(idx)/2));
        case{'Hull'}
            [xdata,ydata] = LatexFilterHull(xbuffer,ybuffer,floor(length(idx)/2));
        otherwise
            ydata = ybuffer(idx);
            xdata = xbuffer(idx);
    end
    texdata(ii).x = xdata;
    texdata(ii).y = ydata;
    texdata(ii).ident = [latexfnames{ii} postfix];
end

if(nargin<4)
    [texname,texpname] = uiputfile('*.tex','Save Tex-File as ...','data');
    filename = [texpname texname(1:end-4)];
end
if(ischar(filename))
    if(exist('options','var'))
        export2latex(texdata,filename,options);
    else
        export2latex(texdata,filename);
    end
else
    error('no savefile chosen or illegal name');
end

% output info
num = length(fnames);
disp([num2str(length(fnames)) ' data sets exported.']);
disp(['x-data name:']);
disp(['''' xname '''']);
disp(['y-data name:']);
for ii=1:length(latexfnames)
    fprintf(['''' latexfnames{ii} '''' ' ']);
end
fprintf(['\n']);