forked from plotly/plotly_matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotlygenimage.m
More file actions
69 lines (59 loc) · 2.17 KB
/
plotlygenimage.m
File metadata and controls
69 lines (59 loc) · 2.17 KB
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
function plotlygenimage(figure_or_data, filename, varargin)
[pathstr, name, ext] = fileparts(filename);
if nargin < 3
format = ext(2:length(ext));
else
format = varargin{1};
end
if (strcmp(ext,'') && nargin < 3)
filename = [filename, '.png'];
format = 'png';
elseif( ~strcmp(ext, '') && nargin < 3)
format = ext(2:length(ext));
elseif(strcmp(ext,'') && nargin==3)
filename = [filename, '.', varargin{1}];
else
filename = [filename, '.', varargin{1}];
end
if isstruct(figure_or_data)
figure = figure_or_data;
elseif iscell(figure_or_data)
figure = struct('data', data);
end
body = struct('figure', figure, 'format', format);
payload = m2json(body);
[un, key, domain] = signin;
url = [domain, '/apigenimage/'];
headers = struct(...
'name',...
{...
'plotly-username',...
'plotly-apikey',...
'plotly-version',...
'plotly-platform',...
'user-agent'
},...
'value',...
{...
un,...
key,...
plotly_version,...
'MATLAB',...
'MATLAB'
});
% return the response as bytes -
% convert the bytes to unicode chars if the response fails or isn't (pdf, png, or jpeg)
% ... gnarly!
[response_string, extras] = urlread2(url, 'Post', payload, headers, 'CAST_OUTPUT', false);
if( extras.status.value ~= 200 || ...
~(strcmp(extras.allHeaders.Content_Type, 'image/jpeg') || ...
strcmp(extras.allHeaders.Content_Type, 'image/png') || ...
strcmp(extras.allHeaders.Content_Type, 'application/pdf')))
response_string = native2unicode(response_string);
end
response_handler(response_string, extras);
image_data = response_string;
fileID = fopen(filename, 'w');
fwrite(fileID, image_data);
fclose(fileID);
end