-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiopen.m
244 lines (234 loc) · 8.8 KB
/
uiopen.m
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
function uiopen(type,direct)
%UIOPEN Present file selection dialog with appropriate file filters.
%
% UIOPEN presents a file selection dialog. The user can either choose a
% file to open or click cancel. No further action is taken if the user
% clicks on cancel. Otherwise the OPEN command is evaluated in the base
% workspace with the user specified filename.
%
% These are the file filters presented using UIOPEN.
%
% 1st input argument Filter List
% <no input args> *.m, *.fig, *.mat,
% *.mdl, *.slx (if Simulink is installed),
% *.cdr (if Stateflow is installed),
% *.rtw, *.tmf, *.tlc, *.c, *.h, *.ads, *.adb
% (if Simulink Coder is installed),
% *.*
% MATLAB *.m, *.fig, *.*
% LOAD *.mat, *.*
% FIGURE *.fig, *.*
% SIMULINK *.mdl, *.slx, *.*
% EDITOR *.m, *.mdl, *.cdr, *.rtw, *.tmf, *.tlc, *.c, *.h, *.ads, *.adb, *.*
%
% If the first input argument is unrecognized, it is treated as a file
% filter and passed directly to the UIGETFILE command.
%
% If the second input argument is true, the first input argument is
% treated as a filename.
%
% Examples:
% uiopen % displays the dialog with the file filter set to all MATLAB
% %files.
%
% uiopen('matlab') %displays the dialog with the file
% %filter set to all MATLAB files.
%
% uiopen('load') %displays the dialog with the file filter set to
% %MAT-files (*.mat).
%
% uiopen('figure') %displays the dialog with the file filter set to
% %figure files (*.fig).
%
% uiopen('simulink') %displays the dialog with the file filter set to
% %model files (*.mdl,*.slx).
%
% uiopen('editor') %displays the dialog with the file filter set to
% %"All MATLAB files". This filters out binary
% %formats: .mat, .fig, .slx.
% %All files are opened in the MATLAB Editor.
%
% See also UIGETFILE, UIPUTFILE, OPEN, UIIMPORT.
% Copyright 1984-2011 The MathWorks, Inc.
if regexp(type, '.*\.vff$')
I = vffread(type);
assignin('caller', 'I', I);
return;
elseif regexp(type, '.*\.vtk')
[S.vertices, S.faces] = read_vtk(type);
S.filename = type;
assignin('caller', 'S', S);
return;
elseif regexp(type, '.*\.tif')
I = load_tif(type);
assignin('caller', 'I', I);
return;
elseif regexp(type, '.*\.h5')
I.info = h5info(type);
I.img = h5read(type, ['/', I.info.Datasets(1).Name]);
assignin('caller', 'I', I);
return;
end
if nargin == 0
type = '';
end
if nargin < 2
direct = false;
end
if direct
fn = type;
else
% Error if MATLAB is running in no JVM mode.
warnfiguredialog('uiopen');
if isempty(type)
% Do not provide a filter list. UIGETFILE called below will pick up all
% the filters available in the MATLAB installation automatically.
filterList = '';
else
allML = getPatternAndDescription(com.mathworks.mwswing.FileExtensionFilterUtils.getMatlabProductFilter());
switch(lower(type))
case 'matlab'
filterList = [
allML; ...
getPatternAndDescription(com.mathworks.mwswing.FileExtensionFilterUtils.getMatlabFileFilter()); ...
getPatternAndDescription(com.mathworks.mwswing.FileExtensionFilterUtils.getFigFileFilter()); ...
{'*.*', getString(message('MATLAB:uistring:uiopen:AllFiles'))}
];
case 'load'
filterList = [
getPatternAndDescription(com.mathworks.mwswing.FileExtensionFilterUtils.getMatFileFilter()); ...
allML; ...
{'*.*', getString(message('MATLAB:uistring:uiopen:AllFiles'))}
];
case 'figure'
filterList = [
getPatternAndDescription(com.mathworks.mwswing.FileExtensionFilterUtils.getFigFileFilter()); ...
allML; ...
{'*.*', getString(message('MATLAB:uistring:uiopen:AllFiles'))}
];
case 'simulink'
% Simulink filters are the only ones hardcoded here.
% This should be changed this in future
filterList = [
{'*.mdl;*.slx', getString(message('MATLAB:uistring:uiopen:ModelFiles'))}; ...
allML; ...
{'*.*', getString(message('MATLAB:uistring:uiopen:AllFiles'))}
];
case 'editor'
% We should be deprecating this usage.
% uiopen('editor') is an unused option and does not scale well to new file extenstions
% This option primarily used to open a file in the
% MATLAB Editor using the EDIT function
% According to the documentation we need to remove .mat, .fig, .slx from the list
allMLWithoutBinary = {regexprep(allML{1}, {'*.slx;','*.mat;','*.fig;'}, ''), allML{2}};
filterList = [
allMLWithoutBinary;...
{'*.*', getString(message('MATLAB:uistring:uiopen:AllFiles'))}
];
otherwise
filterList = type;
end
end
% Is it a .APP or .KEY directory on the Mac?
% If so, open it properly.
if strncmp(computer,'MAC',3) && ~iscell(filterList)...
&& (ischar(filterList) && isdir(filterList))
[~, ~, ext] = fileparts(filterList);
if strcmpi(ext, '.app') || strcmpi(ext, '.key')
unix(['open "' filterList '" &']);
return;
end
end
[fn,pn] = uigetfile(filterList,getString(message('MATLAB:uistring:uiopen:DialogOpen')));
if isequal(fn,0)
return;
end
fn = fullfile(pn,fn);
end
try
% send open requests from editor back to editor
if strcmpi(type,'editor')
edit(fn);
else
% Is it a MAT-file?
[~, ~, ext] = fileparts(fn);
if strcmpi(ext, '.mat')
quotedFile = ['''' strrep(fn, '''', '''''') ''''];
evalin('caller', ['load(' quotedFile ');']);
setStatusBar(~isempty(whos('-file', fn)));
return;
end
% Look to see if it's an HDF file If so, don't try to handle it;
% Pass it off to tools that know what to do.
out = [];
fid = fopen(fn);
if fid ~= -1
out = fread(fid, 4);
fclose(fid);
end
if length(out) == 4 && sum(out == [14; 3; 19; 1]) == 4
% Filter out hdftool deprecation warning
warnState = warning('off','MATLAB:imagesci:hdftool:FunctionToBeRemoved');
warnCleanup = onCleanup(@() warning(warnState));
hdftool(fn);
else
sans = [];
% If open creates variables, ans will get populated in here.
% We need to assign it in the calling workspace later
open(fn);
if ~isempty(sans)
vars = sans;
% Shove the resulting variables into the calling workspace
status = true;
if isstruct(vars)
names = fieldnames(vars);
status = ~isempty(names);
for i = 1:length(names)
assignin('caller',names{i}, vars.(names{i}));
end
else
assignin('caller','ans',vars);
end
setStatusBar(status);
end
end
end
catch ex
% Strip hyperlinks since errordlg does not support them
err = ex.getReport('basic','hyperlinks','off');
try
err = regexprep(err, '<a\s+href\s*=\s*"[^"]*"[^>]*>(.*?)</a>','$1');
catch
% Just try removing hyperlinks that are left over
end
errordlg(err);
end
end
function setStatusBar(varsCreated)
if varsCreated
theMessage = getString(message('MATLAB:uistring:uiopen:VariablesCreatedInCurrentWorkspace'));
else
theMessage = getString(message('MATLAB:uistring:uiopen:NoVariablesCreatedInCurrentWorkspace'));
end
% The following class reference is undocumented and
% unsupported, and may change at any time.
dt = javaMethod('getInstance', 'com.mathworks.mde.desk.MLDesktop');
if dt.hasMainFrame
dt.setStatusText(theMessage);
else
disp(theMessage);
end
end
function filters = getPatternAndDescription(javaFileExtensionFilters)
filters = cell(1,2);
% PATTERNS
pattern = javaFileExtensionFilters.getPatterns;
if length(pattern)>1
cellPatterns = arrayfun(@(x) [char(x) ';'], pattern,'UniformOutput',false);
filters{1,1} = [cellPatterns{:}];
else
filters{1,1} = char(pattern);
end
% DESCRIPTIONS
filters{1,2} = char(javaFileExtensionFilters.getDescription);
end