1 %> @ingroup guigroup mainguis codegen
3 %> @brief Tool to merge several single-spectrum files into a dataset.
4 %> @image html Screenshot-mergetool.png
6 %> <b>Directory containing multiple files</b> - directory containing multiple single-spectrum files.
7 %> @note make sure that
this directory contains <b>only</b> the files of interest. View files, sort by <b>size</b> and checks extremities to detect unusual
8 %> file sizes. These may be invalid spectra.
9 %> @attention Never use directory names nor file names containing single quotes (
" ' "). They are not handled properly by mergetool, and will cause the operation to fail.
11 %> <b>File type</b> - Currently supported types are:
12 %> @arg
"Pirouette .DAT" text format
13 %> @arg OPUS binary format
14 %> @arg Wire TXT format
16 %> <b>File filter</b> - wildcard filter. Examples: <code>*.*</code>; <code>*.dat</code>; <code>*.DAT</code>
18 %> <b>Group code trimming dot (right-to-left)</b> - allows you control over trimming off part of the filename
for the <b>group code</b> of each spectrum.
19 %> For example, you may have several files like:
26 %> | 1 first dot (left-to-right)
30 %> All these are spectra from the same group (named
"sample1"). Specifying
"2" for the trimming dot will
get rid of
31 %> everything after the 2nd last dot counting from right to left. Thus, all spectra will have group code
"sample1".
33 %> <h3>Image building options</h3>
34 %> If <b>Build image is checked</b>, the image <b>height</b> needs to be informed. The width is automatically calculated
as the number of files in the directory divided by the informed <b>height</b>.
35 %> @note To build an image, the files need to have a sequential numbering between dots,
as above.
38 function varargout = mergetool(varargin)
39 % Last Modified by GUIDE v2.5 24-Nov-2012 11:42:47
41 % Begin initialization code - DO NOT EDIT
43 gui_State =
struct(
'gui_Name', mfilename, ...
44 'gui_Singleton', gui_Singleton, ...
45 'gui_OpeningFcn', @mergetool_OpeningFcn, ...
46 'gui_OutputFcn', @mergetool_OutputFcn, ...
47 'gui_LayoutFcn', [] , ...
49 if nargin && ischar(varargin{1})
50 gui_State.gui_Callback = str2func(varargin{1});
54 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
56 gui_mainfcn(gui_State, varargin{:});
58 % End initialization code - DO NOT EDIT
61 % --- Executes just before mergetool is made visible.
62 function mergetool_OpeningFcn(hObject, eventdata, handles, varargin) %#ok<*INUSL>
64 % Choose default command line output for mergetool
65 handles.output = hObject;
70 set(handles.editPath,
'String', PATH.data_spectra);
71 guidata(hObject, handles); % Update handles structure
76 % --- Outputs from this function are returned to the command line.
77 function varargout = mergetool_OutputFcn(hObject, eventdata, handles)
78 varargout{1} = handles.output;
81 %=======================================================================================================================
84 %==============================
85 function handles = find_handles()
86 H = findall(0, 'Name', 'mergetool');
92 %==============================
94 handles = find_handles();
95 a = get(handles.editStatus2, 'String');
97 set(handles.editStatus2,
'String', [a; s]);
99 %==============================
100 function morasse_lah(handles, filename, idx1, idx2, msg)
103 add_log([repmat(' ', 1, idx1-1), repmat('-', 1, idx2-idx1+1)]);
104 add_log([repmat(' ', 1, idx2-1), '|']);
105 add_log([repmat(' ', 1, idx2-1), msg]);
109 %==============================
110 % Reads parameters from mergetool window edit boxes; performs minor checks
112 % Returns a stru with .filetype, .wild, .trimdot, .flag_image, .height
113 function stru = get_window_settings()
114 handles = find_handles();
115 stypes = {
'pir',
'opus',
'wire'};
117 stru.filetype = stypes{
get(handles.popupmenu_type,
'value')};
118 stru.wild = get_wild();
119 stru.trimdot = eval(
get(handles.editTrimdot,
'String'));
120 stru.flag_image =
get(handles.checkbox_flag_image,
'Value');
121 stru.height = eval(
get(handles.edit_height,
'String'));
123 if isempty(stru.height)
129 irerror('Please specify height');
133 irerror('For image building, "Group code trimming dot" needs be at least 1!');
137 irerror(sprintf('Error in window settings:\n%s', ME.message));
140 %==============================
141 function do_checks(handles)
142 set(handles.editStatus2, 'String', '');
143 add_log('==> Checking... ==>');
145 stru = get_window_settings();
147 [filenames, groupcodes] =
resolve_dir(stru.wild, stru.trimdot, stru.flag_image);
148 no_files = numel(filenames);
150 add_log(sprintf('Number of files: %d', no_files));
151 add_log(sprintf('Number of groups: %d', numel(unique(groupcodes))));
154 filename = filenames{1};
155 idxs = find([filename,
'.'] ==
'.');
156 if numel(idxs) < stru.trimdot+1
157 irerror(sprintf('File name such
as "%s" has only %d dot%s, whereas "Group code trimming dot" is %d!', filename, numel(idxs)-1,
iif(numel(idxs)-1 > 1, 's', ''), stru.trimdot));
160 idx2 = idxs(end-stru.trimdot)-1;
161 code = filename(1:idx2);
162 morasse_lah(handles, filename, idx1, idx2, [
'Group code ("', code,
'")']);
165 idx1 = idxs(end-stru.trimdot)+1;
166 idx2 = idxs(end-stru.trimdot+1)-1;
167 sorderref = filename(idx1:idx2);
168 morasse_lah(handles, filename, idx1, idx2, [
'Sequence number for image pixels ("', sorderref,
'")']);
169 orderref = str2double(sorderref);
171 irerror(sprintf('File part "%s" should be a number!', sorderref));
176 irerror('Please specify image height!');
179 if no_files/stru.height ~= floor(no_files/stru.height)
180 irerror(sprintf('Invalid image height: %d not divisible by %d!', no_files, stru.height));
183 add_log(sprintf('Image width: %d', no_files/stru.height));
190 [filenames, groupcodes] =
resolve_dir(stru.wild, stru.trimdot, stru.flag_image); %
#ok<NASGU>
192 add_log(
'===> ...Passed!');
194 add_log([
'===> ...Checking error: ', ME.message]);
199 function path_ = get_wild()
200 handles = find_handles();
201 % handles structure with handles and user data (see GUIDATA)
203 path_ = get(handles.editPath, 'String');
204 filter = get(handles.editFilter, 'String');
212 path_ = [path_ filter];
215 %=======================================================================================================================
223 function editPath_Callback(hObject, eventdata, handles)
224 set(handles.textStatus, 'String', '?');
225 set(handles.editStatus2, 'String', '?');
228 function pushbuttonGo_Callback(hObject, eventdata, handles)
229 set(handles.editStatus2, 'String', '');
232 % Pre-check with get_wild() only
235 if length(a) == 0 %
#ok<ISMT>
236 msgbox(
'No files found in specified directory!');
239 % Check-n-load vars from GUI
240 stru = get_window_settings();
242 [filenames, groupcodes] =
resolve_dir(stru.wild, stru.trimdot, stru.flag_image); %#ok<NASGU>
243 no_files = numel(filenames);
245 % Creates dataset in workspace
248 s_code = sprintf(
'[%s, flag_error] = %s2data(''%s'', %d, %d, %d);', ...
249 name_new, stru.filetype, path_, stru.trimdot, stru.flag_image, stru.height); % Line of MATLAB code
251 evalin(
'base', s_code);
253 ds = evalin(
'base', [name_new
';']);
254 flag_error = evalin(
'base',
'flag_error;');
256 add_log(sprintf(
'Imported: %d', no_files-flag_error));
257 add_log(sprintf(
'Failed: %d', flag_error));
258 add_log(sprintf(
'Total: %d', no_files));
260 ss =
'Finished with errors. Check MATLAB command window output.';
261 if flag_error > 7 && flag_error/no_files > 0.51
262 add_log(sprintf(
'A large amount of files failed to import. Maybe the files are of a different type?'));
267 add_log(sprintf(
'%s! Variable name in workspace: %s; Number of rows: %d', ss, name_new, ds.no));
270 global PATH; %#ok<TLEV>
271 PATH.data_spectra =
get(handles.editPath,
'String');
280 function pushbuttonOpen_Callback(hObject, eventdata, handles) %#ok<*DEFNU>
281 o = uigetdir(
get(handles.editPath,
'String'));
283 if ~(isnumeric(o) && o == 0)
284 set(handles.editPath,
'String', o);
288 function pushbuttonLaunch_Callback(hObject, eventdata, handles)
292 function pushbutton_check_Callback(hObject, eventdata, handles)
296 function pushbuttonLaunchObjtool_Callback(hObject, eventdata, handles)
300 function pushbutton_detect_Callback(hObject, eventdata, handles)
303 handles = find_handles();
304 i_type = find(strcmp(s, {
'pir',
'opus',
'wire'}));
305 set(handles.popupmenu_type,
'Value', i_type);
306 add_log(sprintf(
'Detected file type: "%s"', s));
308 msgbox(
'Could not detect file type in specified directory.');
311 %------------------------------------------------------------------------------------------------------------------------------------------
313 function editFilter_Callback(hObject, eventdata, handles) %#ok<*INUSD>
314 function editFilter_CreateFcn(hObject, eventdata, handles)
315 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
316 set(hObject,'BackgroundColor','white');
318 function editTrimdot_Callback(hObject, eventdata, handles)
319 function editTrimdot_CreateFcn(hObject, eventdata, handles)
320 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
321 set(hObject,'BackgroundColor','white');
325 function editStatus2_Callback(hObject, eventdata, handles)
326 function editStatus2_CreateFcn(hObject, eventdata, handles)
327 function checkbox_flag_image_Callback(hObject, eventdata, handles)
328 function edit_height_Callback(hObject, eventdata, handles)
329 function edit_height_CreateFcn(hObject, eventdata, handles)
330 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
331 set(hObject,'BackgroundColor','white');
333 function popupmenu_type_Callback(hObject, eventdata, handles)
334 function popupmenu_type_CreateFcn(hObject, eventdata, handles)
335 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
336 set(hObject,'BackgroundColor','white');
338 function editPath_CreateFcn(hObject, eventdata, handles)
339 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
340 set(hObject,'BackgroundColor','white');
342 function editTarget_Callback(hObject, eventdata, handles)
343 function editTarget_CreateFcn(hObject, eventdata, handles)
344 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
345 set(hObject,'BackgroundColor','white');
function send_error(in ME)
function resolve_dir(in wild, in trimdot, in flag_image)
function detect_spectrum_type(in wild)
function iif(in cond, in x1, in x2)
function gui_set_position(in hObject)
Analysis Session (AS) base class.
function find_varname(in prefix)