1+ function [data , layout ] = convertFigure(f )
2+ % convertFigure - converts a matlab figure object into data and layout
3+ % plotly structs.
4+ % [data, layout] = convertFigure(f)
5+ % f - root figure object in the form of a struct. Use f = get(gcf); to
6+ % get the current figure struct.
7+ % data - a cell containing plotly data structs
8+ % layout - a plotly layout struct
9+ %
10+ % For full documentation and examples, see https://plot.ly/api
11+
12+
13+ axis_num = numel(f .Children );
14+
15+ if ~strcmp(' figure' , f .Type )
16+ error(' Input object is not a figure' )
17+ end
18+
19+ if axis_num == 0
20+ error(' Input figure object is empty!' )
21+ end
22+
23+ % placeholders
24+ data = {};
25+ data_counter = 1 ;
26+ annotations = {};
27+ annot_counter = 1 ;
28+ bar_counter = 0 ;
29+ layout = {};
30+ legend= {};
31+ x_axis= {};
32+ y_axis= {};
33+
34+ % copy general layout fields
35+ layout = extractLayoutGeneral(f , layout );
36+
37+ % For each axes
38+ % TEMP: reverse order of children
39+ for i= axis_num : -1 : 1
40+ m_axis = get(f .Children(i ));
41+ % TODO:do something about this add/replace thing...
42+ if strcmp(' legend' ,m_axis .Tag )
43+ legend = extractLegend(m_axis );
44+ else
45+ % TODO:do something about this add/replace thing...
46+ if strcmp(' axes' ,m_axis .Type ) % %&& (strcmp('replace',m_axis.NextPlot) || strcmp('new',m_axis.NextPlot))
47+ [xid , yid , x_axis y_axis ] = extractAxes(m_axis , layout , x_axis , y_axis );
48+ m_title = get(m_axis .Title );
49+ annot_tmp = extractTitle(m_title , x_axis{xid }, y_axis{yid });
50+ if numel(annot_tmp )>0
51+ annotations{annot_counter } = annot_tmp ;
52+ annot_counter = annot_counter + 1 ;
53+ end
54+ data_num = numel(m_axis .Children );
55+ if data_num > 0
56+ % For each data object in a given axes
57+ for j= 1 : data_num
58+ m_data = get(m_axis .Children(j ));
59+ % display(['Data child ' num2str(j) ' is of type ' m_data.Type])
60+
61+ if strcmp(' line' ,m_data .Type )
62+ % line scatter plot
63+ data{data_counter } = extractDataScatter(m_data , xid , yid , m_axis .CLim , f .Colormap );
64+ data_counter = data_counter + 1 ;
65+ end
66+ if strcmp(' text' ,m_data .Type )
67+ % annotation
68+ annot_tmp = extractDataAnnotation(m_data , xid , yid );
69+ if numel(annot_tmp )>0
70+ annotations{annot_counter } = annot_tmp ;
71+ annot_counter = annot_counter + 1 ;
72+ end
73+
74+ end
75+ if strcmp(' patch' ,m_data .Type )
76+ % area plot
77+ data{data_counter } = extractDataScatter(m_data , xid , yid , m_axis .CLim , f .Colormap );
78+ data{data_counter } = parseFill(m_data , data{data_counter }, m_axis .CLim , f .Colormap );
79+ data_counter = data_counter + 1 ;
80+ end
81+ if strcmp(' hggroup' ,m_data .Type )
82+
83+ % TODO: improve condition to differentiate between
84+ % scatter and bar chart
85+ if isfield(m_data , ' BarLayout' )
86+ display(' hggroup bar plot!' )
87+ [data{data_counter } layout ] = extractDataBar(m_data , layout , xid , yid , m_axis .CLim , f .Colormap );
88+ data_counter = data_counter + 1 ;
89+ % copy in bar gaps
90+ layout.bargap = 1 - m_data .BarWidth ;
91+ layout.barmode = m_data .BarLayout(1 : end - 2 );
92+ bar_counter = bar_counter + 1 ;
93+ else
94+ if isfield(m_data , ' Marker' ) && numel(m_data .Marker )>0
95+ % scatter plot
96+ data{data_counter } = extractDataScatter(m_data , xid , yid , m_axis .CLim , f .Colormap );
97+ data_counter = data_counter + 1 ;
98+ end
99+ if isfield(m_data , ' EdgeColor' ) && isfield(m_data , ' FaceColor' )
100+ % area plot
101+ data{data_counter } = extractDataScatter(m_data , xid , yid , m_axis .CLim , f .Colormap );
102+ data{data_counter } = parseFill(m_data , data{data_counter }, m_axis .CLim , f .Colormap );
103+ data_counter = data_counter + 1 ;
104+ end
105+ end
106+
107+ end
108+
109+ if strcmp(' text' ,m_data .Type )
110+ annot_tmp = extractDataAnnotation(m_data , xid , yid );
111+ if numel(annot_tmp )>0
112+ annotations{annot_counter } = annot_tmp ;
113+ annot_counter = annot_counter + 1 ;
114+ end
115+
116+ end
117+
118+
119+ end
120+ end
121+
122+
123+ end
124+ end
125+ end
126+
127+ % BAR MODIFY
128+ if bar_counter > 1 && strcmp(layout .barmode , ' group' )
129+ layout.bargroupgap = layout .bargap ;
130+ layout.bargap = 0.3 ;
131+ end
132+
133+ % ANNOTATIONS
134+ layout.annotations = annotations ;
135+
136+
137+ % LEGEND
138+ if numel(legend )==0
139+ layout.showlegend = false ;
140+ else
141+ layout.legend = legend ;
142+ layout.showlegend = true ;
143+ end
144+
145+
146+ % Assemble axis
147+ for i = 1 : numel(x_axis )
148+ if i == 1
149+ eval(' layout.xaxis=x_axis{1};' )
150+ else
151+ eval([' layout.xaxis' num2str(i ) ' =x_axis{' num2str(i ) ' };' ])
152+ end
153+ end
154+
155+ for i = 1 : numel(y_axis )
156+ if i == 1
157+ eval(' layout.yaxis=y_axis{1};' )
158+ else
159+ eval([' layout.yaxis' num2str(i ) ' =y_axis{' num2str(i ) ' };' ])
160+ end
161+ end
162+
163+
164+ end
0 commit comments