comparison m-toolbox/classes/@LTPDAworkbench/cb_importWBfromObjects.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % CB_IMPORTFROMOBJECT import pipelines from an object in the workspace.
2 %
3 % CALL: LTPDAworkbench.cb_importWBfromObjects
4 %
5 % M Hewitson 13-11-08
6 %
7 % $Id: cb_importWBfromObjects.m,v 1.4 2010/08/06 19:10:48 ingo Exp $
8 %
9 function cb_importWBfromObjects(varargin)
10
11 wb = varargin{1};
12
13 % Display an option dialog
14 % - from workspace
15 % - from file
16 warning('off', 'MATLAB:JavaEDTAutoDelegation');
17 cd = mpipeline.dialogs.FileOrWorkspaceDialog(wb.mp, true);
18 cd.setVisible(true);
19 warning('on', 'MATLAB:JavaEDTAutoDelegation');
20
21 xml = '';
22 if ~cd.isCancelled
23
24 choice = cd.getChoice;
25
26 if strcmp(choice, 'workspace')
27
28 [objs, varnames] = getWorkspaceObjs();
29
30 % check we have a least one object with an attached workbench
31 haveWB = false;
32 for kk=1:numel(objs)
33 for ll=1:numel(objs{kk})
34 if ~isempty(objs{kk}(ll).mdlfile)
35 haveWB = true;
36 end
37 end
38 end
39
40 if ~haveWB
41 utils.helper.errorDlg('The workspace contains no LTPDA objects with attached workbenches.', 'No workbench found');
42 end
43
44 % Add all objects to the workspace viewer that have an attached
45 % workbench file.
46 wd = mpipeline.dialogs.WorkspaceLister(wb.mp,true);
47 for kk=1:numel(varnames)
48 for ll=1:numel(objs{kk})
49 objs{kk}(ll).mdlfile
50 if ~isempty(objs{kk}(ll).mdlfile)
51 wd.addObject(varnames(kk).name, varnames(kk).class, char(objs{kk}(ll)));
52 end
53 end
54 end
55
56 % launch the dialog
57 warning('off', 'MATLAB:JavaEDTAutoDelegation');
58 wd.setVisible(true);
59 warning('on', 'MATLAB:JavaEDTAutoDelegation');
60
61 % check what the user did
62 if ~wd.isCancelled
63 % get the selected object
64 wsobj = wd.getSelection;
65 obj = [];
66 if ~isempty(wsobj)
67 % look for the matching object in the MATLAB workspace
68 for kk=1:numel(objs)
69 if strcmp(varnames(kk).name, char(wsobj.getName)) && ...
70 strcmp(varnames(kk).class, char(wsobj.getObjectclass))
71 obj = objs{kk};
72 end
73 end
74 end
75 if ~isempty(obj)
76 % get xml file
77 xml = obj.mdlfile;
78 end
79 end
80
81
82 elseif strcmp(choice, 'file')
83
84 [filename, pathname] = uigetfile({'*.xml', 'Pick an XML object file'; '*.mat', 'Pick a MAT object file'});
85 if isequal(filename,0) || isequal(pathname,0)
86 else
87 fname = fullfile(pathname, filename);
88 try
89 obj = ao(fname);
90 xml = obj.mdlfile;
91 if isempty(xml)
92 error('### AO from file %s has no attached workspace.', fname);
93 end
94
95 catch
96 lasterr
97 error('### Failed to load an AO from the given file.');
98 end
99 end % End empty filename
100 else
101 error('### unknown choice');
102 end
103 end
104
105 % Get the XML string and pass back to the workbench
106 if ~isempty(xml)
107 wb.mp.createPipelinesFromXMLstring(xml);
108 end
109
110 end
111
112 %---------------- Get a list of LTPDA objects in the MATLAB workspace
113 function [objs, varnames] = getWorkspaceObjs()
114
115 % get base workspace variables
116 ws_vars = evalin('base','whos');
117
118 objs = {};
119 varnames = [];
120 for j=1:length(ws_vars)
121 cmd = sprintf('obj = evalin(''base'', ''%s'');', ws_vars(j).name);
122 eval(cmd)
123 if isa(obj, 'ltpda_uo')
124 for kk=1:numel(obj)
125 objs = [objs {obj(kk)}];
126 varnames = [varnames ws_vars(j)];
127 end
128 end
129 end
130 end
131