Skip to content

Commit 69a0772

Browse files
added stripping, and open, with modified input scheme
1 parent 04bf3ae commit 69a0772

11 files changed

+235
-167
lines changed

plotly/convertFigure.m

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
function [data, layout, title] = convertFigure(f)
1+
function [data, layout, title] = convertFigure(f, strip_style)
22
% convertFigure - converts a matlab figure object into data and layout
33
% plotly structs.
44
% [data, layout] = convertFigure(f)
55
% f - root figure object in the form of a struct. Use f = get(gcf); to
66
% get the current figure struct.
7+
% strip_style - boolean, strips all stlying to plotly defaults
78
% data - a cell containing plotly data structs
89
% layout - a plotly layout struct
910
% title - a string with the title of the plot
@@ -34,7 +35,7 @@
3435
title = '';
3536

3637
% copy general layout fields
37-
layout = extractLayoutGeneral(f, layout);
38+
layout = extractLayoutGeneral(f, layout, strip_style);
3839

3940
% For each axes
4041
%TEMP: reverse order of children
@@ -46,9 +47,9 @@
4647
else
4748
%TODO:do something about this add/replace thing...
4849
if strcmp('axes',m_axis.Type) %%&& (strcmp('replace',m_axis.NextPlot) || strcmp('new',m_axis.NextPlot))
49-
[xid, yid, x_axis y_axis] = extractAxes(m_axis, layout, x_axis, y_axis);
50+
[xid, yid, x_axis y_axis] = extractAxes(m_axis, layout, x_axis, y_axis, strip_style);
5051
m_title = get(m_axis.Title);
51-
annot_tmp = extractTitle(m_title, x_axis{xid}, y_axis{yid});
52+
annot_tmp = extractTitle(m_title, x_axis{xid}, y_axis{yid}, strip_style);
5253
if numel(annot_tmp)>0
5354
annotations{annot_counter} = annot_tmp;
5455
annot_counter = annot_counter+1;
@@ -63,12 +64,12 @@
6364

6465
if strcmp('line',m_data.Type)
6566
%line scatter plot
66-
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
67+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap, strip_style);
6768
data_counter = data_counter+1;
6869
end
6970
if strcmp('text',m_data.Type)
7071
%annotation
71-
annot_tmp = extractDataAnnotation(m_data, xid, yid);
72+
annot_tmp = extractDataAnnotation(m_data, xid, yid, strip_style);
7273
if numel(annot_tmp)>0
7374
annotations{annot_counter} = annot_tmp;
7475
annot_counter = annot_counter+1;
@@ -78,8 +79,8 @@
7879

7980
if strcmp('patch',m_data.Type)
8081
%area plot
81-
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
82-
data{data_counter} = parseFill(m_data, data{data_counter}, m_axis.CLim, f.Colormap);
82+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap, strip_style);
83+
data{data_counter} = parseFill(m_data, data{data_counter}, m_axis.CLim, f.Colormap, strip_style);
8384
data_counter = data_counter+1;
8485
end
8586
if strcmp('hggroup',m_data.Type)
@@ -88,7 +89,7 @@
8889
%scatter and bar chart
8990
if isfield(m_data, 'BarLayout')
9091
%bar plot
91-
[data{data_counter} layout] = extractDataBar(m_data, layout, xid, yid, m_axis.CLim, f.Colormap);
92+
[data{data_counter} layout] = extractDataBar(m_data, layout, xid, yid, m_axis.CLim, f.Colormap, strip_style);
9293
data_counter = data_counter+1;
9394
% copy in bar gaps
9495
layout.bargap = 1-m_data.BarWidth;
@@ -97,13 +98,13 @@
9798
else
9899
if isfield(m_data, 'Marker') && numel(m_data.Marker)>0
99100
%scatter plot
100-
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
101+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap, strip_style);
101102
data_counter = data_counter+1;
102103
end
103104
if isfield(m_data, 'EdgeColor') && isfield(m_data, 'FaceColor')
104105
%area plot
105-
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap);
106-
data{data_counter} = parseFill(m_data, data{data_counter}, m_axis.CLim, f.Colormap);
106+
data{data_counter} = extractDataScatter(m_data, xid, yid, m_axis.CLim, f.Colormap, strip_style);
107+
data{data_counter} = parseFill(m_data, data{data_counter}, m_axis.CLim, f.Colormap, strip_style);
107108
data_counter = data_counter+1;
108109
end
109110
end

plotly/fig2plotly.m

+35-14
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
% [response] = fig2plotly()
44
% [response] = fig2plotly(gcf)
55
% [response] = fig2plotly(f)
6-
% [response] = fig2plotly(gcf, plot_name)
7-
% [response] = fig2plotly(f, plot_name)
6+
% [response] = fig2plotly(gcf, 'property', value, ...)
7+
% [response] = fig2plotly(f, 'property', value, ...)
88
% gcf - root figure object in the form of a double.
99
% f - root figure object in the form of a struct. Use f = get(gcf); to
1010
% get the current figure struct.
11-
% plot_name - a string naming the plot
11+
% List of valid properties:
12+
% 'name' - ('untitled')string, name of the plot
13+
% 'strip' - (false)boolean, ignores all styling, uses plotly defaults
14+
% 'open' - (true)boolean, opens a browser window with plot result
1215
% response - a struct containing the result info of the plot
13-
%
16+
%
1417
% For full documentation and examples, see https://plot.ly/api
1518

1619
%default input
1720
f = get(gcf);
1821
plot_name = 'untitled';
22+
strip_style = false;
23+
open_browser = true;
1924

2025
switch numel(varargin)
2126
case 0
@@ -26,24 +31,36 @@
2631
if isa(varargin{1}, 'struct')
2732
f = varargin{1};
2833
end
29-
plot_name = 'untitled';
30-
case 2
34+
otherwise
3135
if isa(varargin{1}, 'double')
3236
f = get(varargin{1});
3337
end
3438
if isa(varargin{1}, 'struct')
3539
f = varargin{1};
3640
end
37-
plot_name = varargin{2};
38-
otherwise
39-
error('Too many arguments!')
40-
end
41+
if mod(numel(varargin),2)~=1
42+
error('Invalid number of arguments')
43+
else
44+
for i=2:2:numel(varargin)
45+
if strcmp('strip', varargin{i})
46+
strip_style = varargin{i+1};
47+
end
48+
if strcmp('name', varargin{i})
49+
plot_name = varargin{i+1};
50+
end
51+
if strcmp('open', varargin{i})
52+
open_browser = varargin{i+1};
53+
end
54+
end
55+
end
4156

57+
end
58+
4259

4360
%convert figure into data and layout data structures
44-
[data, layout, title] = convertFigure(f);
61+
[data, layout, title] = convertFigure(f, strip_style);
4562

46-
if numel(title)>0
63+
if numel(title)>0 && strcmp('untitled', plot_name)
4764
plot_name = title;
4865
end
4966

@@ -52,7 +69,11 @@
5269
'filename',plot_name, ...
5370
'fileopt', 'overwrite'));
5471

55-
display('Done! Check out your plot at:')
56-
display(response.url)
72+
if open_browser
73+
status = dos(['open ' response.url ' > nul 2> nul']);
74+
if status==1
75+
status = dos(['start ' response.url ' > nul 2> nul']);
76+
end
77+
end
5778

5879
end

plotly/fig2plotly_aux/extractAxes.m

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [xid, yid, x_axis y_axis] = extractAxes(a, layout, x_axis, y_axis)
1+
function [xid, yid, x_axis y_axis] = extractAxes(a, layout, x_axis, y_axis, strip_style)
22
% extractAxes - create an axes struct
33
% [xid, yid, x_axis y_axis] = extractAxes(a, layout, x_axis, y_axis)
44
% a - a data struct from matlab describing an axes
@@ -13,7 +13,9 @@
1313
yaxes={};
1414

1515
%copy over general properties
16-
[xaxes, yaxes] = extractAxesGeneral(a, layout, xaxes, yaxes);
16+
17+
[xaxes, yaxes] = extractAxesGeneral(a, layout, xaxes, yaxes, strip_style);
18+
1719

1820
%OVERLAY CHECK
1921
x_bounds = xaxes.domain;

plotly/fig2plotly_aux/extractAxesGeneral.m

+77-60
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
function [xaxes, yaxes] = extractAxesGeneral(a, layout, xaxes, yaxes)
1+
function [xaxes, yaxes] = extractAxesGeneral(a, layout, xaxes, yaxes, strip_style)
22
% extractAxesGeneral - copy general axes struct attributes
33
% [xaxes, yaxes] = extractAxesGeneral(a, layout, xaxes, yaxes)
44
% a - a data struct from matlab describing an axes
55
% layout - a plotly layout strcut
66
% x_axis, y_axis - axis objects
7-
%
7+
%
88
% For full documentation and examples, see https://plot.ly/api
99

1010
%POSITION
11-
xaxes.domain = [a.Position(1) a.Position(1)+a.Position(3)];
12-
yaxes.domain = [a.Position(2) a.Position(2)+a.Position(4)] ...
13-
*(layout.height-layout.margin.t)/layout.height;
11+
if ~strip_style
12+
xaxes.domain = [a.Position(1) a.Position(1)+a.Position(3)];
13+
yaxes.domain = [a.Position(2) a.Position(2)+a.Position(4)] ...
14+
*(layout.height-layout.margin.t)/layout.height;
15+
else
16+
xaxes.domain = [a.Position(1) a.Position(1)+a.Position(3)];
17+
yaxes.domain = [a.Position(2) a.Position(2)+a.Position(4)];
18+
end
1419
if yaxes.domain(1)>1
1520
yaxes.domain(1)=1;
1621
end
@@ -20,55 +25,64 @@
2025
xaxes.side = a.XAxisLocation;
2126
yaxes.side = a.YAxisLocation;
2227

23-
%TICKS
24-
if strcmp(a.TickDir, 'in')
25-
xaxes.ticks = 'inside';
26-
yaxes.ticks = 'inside';
27-
else
28-
xaxes.ticks = 'outside';
29-
yaxes.ticks = 'outside';
30-
end
31-
total_length = max(layout.height*(yaxes.domain(2)-yaxes.domain(1)), ...
32-
layout.width*(xaxes.domain(2)-xaxes.domain(1)))*a.TickLength(1);
33-
xaxes.ticklen = total_length;
34-
yaxes.ticklen = total_length;
35-
if strcmp(a.Box,'on')
36-
xaxes.mirror = 'ticks';
37-
yaxes.mirror = 'ticks';
38-
else
39-
xaxes.mirror = false;
40-
yaxes.mirror = false;
28+
if ~strip_style
29+
30+
%TICKS
31+
if strcmp(a.TickDir, 'in')
32+
xaxes.ticks = 'inside';
33+
yaxes.ticks = 'inside';
34+
else
35+
xaxes.ticks = 'outside';
36+
yaxes.ticks = 'outside';
37+
end
38+
total_length = max(layout.height*(yaxes.domain(2)-yaxes.domain(1)), ...
39+
layout.width*(xaxes.domain(2)-xaxes.domain(1)))*a.TickLength(1);
40+
xaxes.ticklen = total_length;
41+
yaxes.ticklen = total_length;
42+
if strcmp(a.Box,'on')
43+
xaxes.mirror = 'ticks';
44+
yaxes.mirror = 'ticks';
45+
else
46+
xaxes.mirror = false;
47+
yaxes.mirror = false;
48+
end
49+
50+
%TODO: should this multiplier remain?
51+
if strcmp(a.FontUnits, 'points')
52+
xaxes.tickfont.size = 1.3*a.FontSize;
53+
yaxes.tickfont.size = 1.3*a.FontSize;
54+
end
55+
56+
%LINES
57+
if strcmp(a.XGrid, 'on') || strcmp(a.XMinorGrid, 'on')
58+
xaxes.showgrid = true;
59+
else
60+
xaxes.showgrid = false;
61+
end
62+
if strcmp(a.YGrid, 'on') || strcmp(a.YMinorGrid, 'on')
63+
yaxes.showgrid = true;
64+
else
65+
yaxes.showgrid = false;
66+
end
67+
xaxes.zeroline = false;
68+
yaxes.zeroline = false;
69+
70+
%COLORS
71+
xaxes.linecolor = parseColor(a.XColor);
72+
xaxes.tickcolor = parseColor(a.XColor);
73+
xaxes.tickfont.color = parseColor(a.XColor);
74+
yaxes.linecolor = parseColor(a.YColor);
75+
yaxes.tickcolor = parseColor(a.YColor);
76+
yaxes.tickfont.color = parseColor(a.YColor);
77+
4178
end
4279

43-
%TODO: should this multiplier remain?
44-
if strcmp(a.FontUnits, 'points')
45-
xaxes.tickfont.size = 1.3*a.FontSize;
46-
yaxes.tickfont.size = 1.3*a.FontSize;
47-
end
4880

49-
%LINES
50-
if strcmp(a.XGrid, 'on') || strcmp(a.XMinorGrid, 'on')
51-
xaxes.showgrid = true;
52-
else
53-
xaxes.showgrid = false;
54-
end
55-
if strcmp(a.YGrid, 'on') || strcmp(a.YMinorGrid, 'on')
56-
yaxes.showgrid = true;
57-
else
58-
yaxes.showgrid = false;
59-
end
60-
xaxes.zeroline = false;
61-
yaxes.zeroline = false;
6281

63-
%COLORS
64-
xaxes.linecolor = parseColor(a.XColor);
65-
xaxes.tickcolor = parseColor(a.XColor);
66-
xaxes.tickfont.color = parseColor(a.XColor);
67-
yaxes.linecolor = parseColor(a.YColor);
68-
yaxes.tickcolor = parseColor(a.YColor);
69-
yaxes.tickfont.color = parseColor(a.YColor);
7082

7183
%SCALE
84+
xaxes.type = a.XScale;
85+
yaxes.type = a.YScale;
7286
xaxes.range = a.XLim;
7387
yaxes.range = a.YLim;
7488
if strcmp(a.XDir, 'reverse')
@@ -77,8 +91,7 @@
7791
if strcmp(a.YDir, 'reverse')
7892
yaxes.range = [a.YLim(2) a.YLim(1)];
7993
end
80-
xaxes.type = a.XScale;
81-
yaxes.type = a.YScale;
94+
8295
if strcmp('log', xaxes.type)
8396
xaxes.range = log10(xaxes.range);
8497
end
@@ -106,25 +119,29 @@
106119

107120
%LABELS
108121
if numel(a.XLabel)==1
109-
m_title = get(a.XLabel);
122+
m_title = get(a.XLabel);
110123
if numel(m_title.String)>0
111124
xaxes.title = parseText(m_title.String);
112-
if strcmp(m_title.FontUnits, 'points')
113-
xaxes.titlefont.size = 1.3*m_title.FontSize;
125+
if ~strip_style
126+
if strcmp(m_title.FontUnits, 'points')
127+
xaxes.titlefont.size = 1.3*m_title.FontSize;
128+
end
129+
xaxes.titlefont.color = parseColor(m_title.Color);
114130
end
115-
xaxes.titlefont.color = parseColor(m_title.Color);
116-
end
131+
end
117132
end
118133

119134
if numel(a.YLabel)==1
120-
m_title = get(a.YLabel);
135+
m_title = get(a.YLabel);
121136
if numel(m_title.String)>0
122137
yaxes.title = parseText(m_title.String);
123-
if strcmp(m_title.FontUnits, 'points')
124-
yaxes.titlefont.size = 1.3*m_title.FontSize;
138+
if ~strip_style
139+
if strcmp(m_title.FontUnits, 'points')
140+
yaxes.titlefont.size = 1.3*m_title.FontSize;
141+
end
142+
yaxes.titlefont.color = parseColor(m_title.Color);
125143
end
126-
yaxes.titlefont.color = parseColor(m_title.Color);
127-
end
144+
end
128145
end
129146

130147

plotly/fig2plotly_aux/extractDataAnnotation.m

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function data = extractDataAnnotation(d, xid, yid)
1+
function data = extractDataAnnotation(d, xid, yid, strip_style)
22
% extractDataAnnotation - create a general purpose annotation struct
33
% [data] = extractDataAnnotation(d, xid, yid)
44
% xid,yid - reference axis indices
@@ -25,11 +25,13 @@
2525

2626
%TEXT
2727
data.text = parseText(d.String);
28-
if strcmp(d.FontUnits, 'points')
29-
data.font.size = 1.3*d.FontSize;
28+
if ~strip_style
29+
if strcmp(d.FontUnits, 'points')
30+
data.font.size = 1.3*d.FontSize;
31+
end
32+
data.font.color = parseColor(d.Color);
33+
%TODO: add font type
3034
end
31-
data.font.color = parseColor(d.Color);
32-
%TODO: add font type
3335

3436
%POSITION
3537
%use center of bounding box as reference

0 commit comments

Comments
 (0)