summaryrefslogtreecommitdiff
path: root/modules/compatibility_functions/macros/mtlb_plot.sci
blob: edc2d77d801c06ef74286c8b16fdc4f77b35a661 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function h=mtlb_plot(varargin)
    // Emulation function for Matlab plot()

    // Default for Matlab graphics
    mtlbColorOrder=[..
    0    0    1   ;..
    0    0.5  0   ;..
    1    0    0   ;..
    0    0.75 0.75;..
    0.75 0    0.75;..
    0.75 0.75 0   ;..
    0.25 0.25 0.25].*255;

    // red green blue cyan magenta yellow black white
    mtlbcolors=["r"  ,"g"    ,"b"   ,"c"   ,"m"      ,"y"     ,"k"    ,"w"    ]
    scicolors= ["red","green","blue","cyan","magenta","yellow","black","white"]

    mtlbmarkers=["+","o","*",".","x","s","d","^","v",">","<","p","h"]
    scimarkers= [ 1 , 9 , 3 , 0 , 2 , -1, 5 , 6 , 7 , -1, -1, -1, -1]

    mtlblinestyles=["-","--",":","-."]
    scilinestyles= [ 1 , 2  , -1, 5  ]

    // plot(axes_handle,...)
    if type(varargin(1))==9 then
        set("current_axes",varargin(1))
        varargin(1)=null()
    end


    mtlbColor=mtlbColorOrder(1,:)
    colorIndex=1

    // plot(Y)
    if lstsize(varargin)==1 then
        mtlb_plotY(varargin(1))
    elseif lstsize(varargin)==2 then

        // plot(Y,Linespec)
        if type(varargin(2))==10 then
            mtlb_plotY(varargin(1))
            curh=gce()
            curh=curh.children(1)
            mstyl=linespec2sci(varargin(2),curh)
            if ~isempty(mstyl) then
                mtlb_plotY(varargin(1))
                a=gca()
                markplot=a.children(1)
                markplot.children(1).mark_mode="on"
                markplot.children(1).mark_style=mstyl
            end
            if colorIndex<>7 then
                colorIndex=colorIndex+1;
            else
                colorIndex=1;
            end
            mtlbColor=mtlbColorOrder(colorIndex,:);
            // plot(X,Y)
        else
            mtlb_plotXY(varargin(1),varargin(2))
            if colorIndex<>7 then
                colorIndex=colorIndex+1;
            else
                colorIndex=1;
            end
            mtlbColor=mtlbColorOrder(colorIndex,:);
        end
        // plot(X,Y,Linespec)
    elseif lstsize(varargin)==3 & type(varargin(3))==10 then
        mtlb_plotXY(varargin(1),varargin(2))
        curh=gce()
        curh=curh.children(1)
        mstyl=linespec2sci(varargin(3),curh)
        if ~isempty(mstyl) then
            mtlb_plotXY(varargin(1),varargin(2))
            a=gca()
            markplot=a.children(1)
            markplot.children(1).mark_mode="on"
            markplot.children(1).mark_style=mstyl
        end
        if colorIndex<>7 then
            colorIndex=colorIndex+1;
        else
            colorIndex=1;
        end
        mtlbColor=mtlbColorOrder(colorIndex,:);
    else
        k=1
        while k<=lstsize(varargin)
            if k<lstsize(varargin) & type(varargin(k))<>10 then
                mtlb_plotXY(varargin(k),varargin(k+1))
                set(gca(),"auto_clear","off")
                X=varargin(k)
                Y=varargin(k+1)
                lineplot=gce()
                markplot=lineplot
                k=k+2
                if colorIndex<>7 then
                    colorIndex=colorIndex+1;
                else
                    colorIndex=1;
                end
                mtlbColor=mtlbColorOrder(colorIndex,:);
            elseif k<=lstsize(varargin) & type(varargin(k))==10 then
                opt=varargin(k)
                opt=convstr(opt,"l")
                if ~isempty(strindex("linestyle",opt)) then
                    curh=lineplot.children(1)
                    linespec2sci(varargin(k+1),curh)
                    k=k+2
                elseif ~isempty(strindex("linewidth",opt)) then
                    lineplot.children(1).thickness=varargin(k+1)
                    k=k+2
                elseif ~isempty(strindex("marker",opt)) then
                    curh=markplot.children(1)
                    linespec2sci(varargin(k+1),curh)
                    k=k+2
                elseif ~isempty(strindex("markeredgecolor",opt)) then
                    warning(msprintf(gettext("%s: ''%s'' option ignored.\n"),"mtlb_plot","MarkerEdgeColor"));
                    k=k+2
                elseif ~isempty(strindex("markerfacecolor",opt)) then
                    curh=markplot.children(1)
                    linespec2sci(varargin(k+1),curh)
                    k=k+2
                elseif ~isempty(strindex("markersize",opt)) then
                    scimarksize=[8 10 12 14 18 24]
                    markplot.children(1).mark_size=scimarksize(max(find(scimarksize<varargin(k+1))))
                    k=k+2
                else
                    curh=lineplot.children(1)
                    mstyl=linespec2sci(varargin(k),curh)
                    if ~isempty(mstyl) then
                        mtlb_plotXY(X,Y)
                        a=gca()
                        markplot=a.children(1)
                        markplot.children(1).mark_mode="on"
                        markplot.children(1).mark_style=mstyl
                    end
                    k=k+1
                end
            end
        end
    end
    a=gca()
    a.auto_clear="on"
    // Log axis ?
    global mtlb_log_mod
    if ~isempty(mtlb_log_mod)
        a.log_flags = mtlb_log_mod;
    end
    h=a.children
endfunction

function mtlb_plotY(Y)
    if norm(Y,1)==0 then
        plot2d(Y)
    else
        plot2d(real(Y),imag(Y))
    end
    e=gce()
    e.children(1).foreground=color(mtlbColor(1),mtlbColor(2),mtlbColor(3))
    e.children(1).line_style=1
endfunction

function mtlb_plotXY(X,Y)
    plot2d(X,Y)
    e=gce()
    e.children(1).foreground=color(mtlbColor(1),mtlbColor(2),mtlbColor(3))
    e.children(1).line_style=1
endfunction

function mark=linespec2sci(linespec,cur_handle)
    mtlbColor=mtlbColor;
    linep=%F
    // line style
    ind=[]
    for k=1:size(mtlblinestyles,"*")
        found=strindex(linespec,mtlblinestyles(k))
        if ~isempty(found) then
            ind=k
            linep=%T
        end
    end
    styl=scilinestyles(ind)
    if styl>=0 then
        cur_handle.line_style=styl
    elseif ~isempty(ind) then
        warning(msprintf(gettext("%s: Unknown line style found in ''%s'' -> set to 6.\n"),"mtlb_plot",linespec));
        cur_handle.line_style=6
    end

    // color
    ind=[]
    for k=1:size(mtlbcolors,"*")
        found=strindex(linespec,mtlbcolors(k))
        if ~isempty(found) then
            ind=k
            break
        end
    end
    clr=scicolors(ind)
    if ~isempty(clr) then
        cur_handle.foreground=color(clr)
        mtlbColor=name2rgb(clr)
    elseif ~isempty(ind) then
        warning(msprintf(gettext("%s: Unknown color found in ''%s'' -> set to black.\n"),"mtlb_plot",linespec));
        cur_handle.foreground=color("black")
        mtlbColor=name2rgb("black")
    end

    // marker
    ind=[]
    for k=1:size(mtlbmarkers,"*")
        found=strindex(linespec,mtlbmarkers(k))
        if ~isempty(found) then
            ind=k
            break
        end
    end
    mark=scimarkers(ind)
    if ~linep & mark>=0 & ~isempty(mark) then
        cur_handle.mark_mode="on"
        cur_handle.mark_style=mark
        cur_handle.mark_size=1
    elseif ~linep & mark>=0 & ~isempty(mark) then
        cur_handle.mark_mode="on"
        cur_handle.mark_style=4
        cur_handle.mark_size=1
    elseif ~isempty(ind) then
        mark=4
    end
    mtlbColor=resume(mtlbColor)
endfunction