1+ function plotlysetup_offline(plotly_bundle_url , varargin )
2+
3+ % CALL: plotlysetup_offline(plotly_bundle_url);
4+ % WHERE: plotly_bundle_url is the plotly bundle url, e.g. http://cdn.plot.ly/plotly-latest.min.js
5+ % If no argument is provided, the default http://cdn.plot.ly/plotly-latest.min.js is used.
6+ % [1] adds plotly api to matlabroot/toolboxes. If successful do [2]
7+ % [2] adds plotly api to searchpath via startup.m of matlabroot and/or userpath
8+
9+ % DEFAULT OUTPUT
10+ exception.message = ' ' ;
11+ exception.identifier = ' ' ;
12+
13+ try % check number of inputs
14+ if (nargin == 0 )
15+ plotly_bundle_url = ' http://cdn.plot.ly/plotly-latest.min.js' ;
16+ elseif (nargin > 1)
17+ error(' plotly:wrongInput' ,....
18+ [' \n\n Whoops! Wrong number of inputs. Please run >> help plotlysetup_offline \n ' ,...
19+ ' for more information regarding the setup your Plotly API MATLAB \n ' ,...
20+ ' Library. Please contact chuck@plot.ly for more information.' ]);
21+ end
22+ catch exception % plotlysetup input problem catch...
23+ fprintf([' \n\n ' exception .identifier exception .message ' \n\n ' ]);
24+ return
25+ end
26+
27+ try
28+ % check to see if plotly is in the searchpath
29+ plotlysetupPath = which(' plotlysetup' );
30+ plotlyFolderPath = fullfile(fileparts(plotlysetupPath ),' plotly' );
31+ % if it was not found
32+ if (strcmp(genpath(plotlyFolderPath ),' ' ))
33+ error(' plotly:notFound' ,...
34+ [' \n\n Shoot! It looks like MATLAB is having trouble finding the current version ' ...
35+ ' \n of Plotly. Please make sure that the Plotly API folder is in the same ' ...
36+ ' \n directory as plotlysetup.m. Questions? chuck@plot.ly\n\n ' ]);
37+ end
38+ % add Plotly API MATLAB Library to search path
39+ addpath(genpath(plotlyFolderPath ));
40+ catch exception % plotly file not found problem catch
41+ fprintf([' \n\n ' exception .identifier exception .message ' \n ' ]);
42+ return
43+ end
44+
45+ if (~is_octave )
46+
47+ try
48+ % embed the api to the matlabroot/toolbox dir.
49+ fprintf(' \n Adding Plotly to MATLAB toolbox directory ... ' );
50+
51+ % plotly folder in the matlab/toolbox dir.
52+ plotlyToolboxPath = fullfile(matlabroot ,' toolbox' ,' plotly' );
53+
54+ if (exist(plotlyToolboxPath ,' dir' )) % check for overwrite...
55+ fprintf([' \n\n [UPDATE]: \n\n Hey! We see that a copy of Plotly has previously been added to\n ' ...
56+ ' your Matlab toolboxes. Would you like us to overwrite it with:\n ' plotlyFolderPath ' ? \n ' ...
57+ ' Careful! You may lose data saved to this Plotly directory.\n\n ' ]);
58+
59+ overwrite = input(' Overwrite (y/n) ? : ' ,' s' );
60+
61+ if (strcmpi(overwrite ,' y' ));
62+ fprintf(' \n [OVERWRITE]:\n\n Overwriting Plotly! ... Done \n ' );
63+ else
64+ fprintf(' \n [NO OVERWRITE]:\n\n Did not overwrite Plotly! ... Done \n ' );
65+ end
66+ else % toolbox Plotly not yet created
67+
68+ % worked (without interuption)...just a formatting thing!
69+ fprintf(' Done\n ' );
70+
71+ % make the plotlyToolboxPath dir.
72+ status = mkdir(plotlyToolboxPath );
73+
74+ % set status to overwrite
75+ overwrite = ' y' ;
76+
77+ % check that the folder was created
78+ if (status == 0 )
79+ error(' plotly:savePlotly' , permissionMessage(' save the Plotly folder' ));
80+ end
81+ end
82+
83+ if (strcmpi(overwrite ,' y' ))
84+
85+ % move a copy of the Plotly api to matlab root directory
86+ [status , msg , messid ] = copyfile(plotlyFolderPath ,plotlyToolboxPath );
87+ % check that the plotly api was copied to the matlab root toolbox directory
88+ if (status == 0 )
89+ if (~strcmp(messid , ' MATLAB:COPYFILE:SourceAndDestinationSame' ))
90+ error(' plotly:copyPlotly' ,permissionMessage(' copy the Plotly folder' ));
91+ end
92+ end
93+
94+ end
95+
96+ % add it to the searchpath (startup.m will handle this next time!)
97+ addpath(genpath(plotlyToolboxPath ),' -end' );
98+
99+ % save plotly api searchpath to startup.m files (only do this if we actually were able to store the api in mtlroot/toolbox!)
100+ fprintf(' Saving Plotly to MATLAB search path via startup.m ... ' );
101+
102+ % check for a startup.m file in matlab rootpath (we want to add one here)
103+ startupFile = [];
104+ startupFileRootPath = fullfile(matlabroot ,' toolbox' ,' local' ,' startup.m' );
105+ if (~exist(startupFileRootPath ,' file' ))
106+ startFileID = fopen(startupFileRootPath , ' w' );
107+ % startup.m does not exist and startupFilePath is non-writable
108+ if (startFileID == - 1 )
109+ error(' plotly:rootStartupCreation' ,permissionMessage(' write the startup.m script' ));
110+ end
111+ startupFile = {startupFileRootPath }; % needed because MATLAB only looks for startup.m when first opened.
112+ end
113+
114+ % check for all startup.m file in searchpath
115+ startupFile = [startupFile ; cell(which(' startup.m' ,' -all' ))];
116+ % write the addpath - plotly api to the startup.m files
117+ [warnings ] = addplotlystartup(startupFile );
118+
119+ % worked!
120+ fprintf(' Done\n ' );
121+
122+ % print any addplotlydstatup warnings;
123+ w = cellfun(@isempty ,warnings );
124+ if (find(~w ))
125+ % output warnings
126+ exception.warnings = warnings ;
127+ fprintf(warnings{find(~w )});
128+ end
129+
130+ catch exception % copying to toolbox/writing to startup.m permission problem catch...
131+ fprintf([' \n\n ' exception .identifier exception .message ' \n\n ' ]);
132+ end
133+
134+ else % if octave
135+ fprintf(' \n\n Octave users: Automatic Plotly API embedding coming soon!\n\n ' );
136+ end % end check for matlab...
137+
138+ % get offline bundle
139+ fprintf(' \n Now downloading the plotly offline bundle ...' );
140+ getplotlyoffline(plotly_bundle_url );
141+
142+ % greet the people!
143+ fprintf(' \n Welcome to Plotly! If you are new to Plotly please enter: >> plotlyhelp to get started!\n\n ' )
144+
145+ end
146+
147+ % helper message function
148+ function message = permissionMessage(spec )
149+ message = [' \n\n Shoot! We tried to ' spec ' to the MATLAB toolbox \n ' ,...
150+ ' directory, but were denied write permission. You'' ll have to add\n ' ,...
151+ ' the Plotly folder to your MATLAB path manually by running: \n\n ' ,...
152+ ' >> plotly_path = fullfile(pwd, '' plotly'' )\n ' ,...
153+ ' >> addpath(genpath(plotly_path))\n\n ' ,...
154+ ' Questions? Chuck@plotly\n\n ' ];
155+ end
156+
157+
0 commit comments