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