summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/numericplots/MatlabFunctions/struct2pst.m
blob: 478ec75d9f8bae8f7c402fa436373473ba1c61a6 (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
function num = struct2pst(data,xname,downsample,filename,postfix,options)
%------------------------------------------------
%2011/09/08 version 1.0
%num = struct2pst(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.
%struct2pstis 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)
%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
%
% 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
fnames = fieldnames(data);
if(nargin<2)    
    [xnamenum,selected] = listdlg('PromptString','Choose x-Axis:','SelectionMode','single','ListString',fnames(1:end));
    if (selected==0)
        error('No x-Axis chosen!');
    end
    xname = fnames{xnamenum};
end

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

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

if(length(idx)>5000)
    buttonname = questdlg(['Warning! Number of points exceeds 5000 and is equal to ' num2str(length(idx)) '! 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 = str2double(answer{1});
            idx = 1:downsample:length(data.(xname));
        case 'No'
            warning(['number of points exceeds 5000 and is equal to ' num2str(length(idx))]);
    end
end


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

% get y-data
jj = 1;
for ii = 1:length(fnames)
    if(strcmp(xname,fnames{ii})~=1 && strcmp('dSPACESettings',fnames{ii})~=1)
        texdata(jj).x = xdata(idx);
        buffer = data.(fnames{ii});
        [nor noc] = size(buffer);
        if(nor>1)
             if(noc>1)
                 error('struct2latex supports only vectors');
             else
                 buffer = buffer';
             end
        end
        texdata(jj).y = buffer(idx);
        texdata(jj).ident = [fnames{ii} postfix];
        jj = jj+1;
    end
end

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

num = jj-1;