Hi,
You can follow the workflow below:
1. Load files
- Load… button → uigetfile opens with MultiSelect on.
- Absolute paths can be stored in handles.fullpath.
2. Populate GUI
3. Let user choose a range
- Pop-ups let the user pick any contiguous block (start ≤ end).
- The chosen indices slice handles.fullpath to create chosen.
4. Batch process
- For each file in chosen:S = load(file); result{k} = S;
- A waitbar shows progress; errors can be caught without killing the loop.
function multiMatImporter
hFig = figure('Name','MAT-file Range Importer',...
'MenuBar','none','ToolBar','none',...
'NumberTitle','off','Resize','off',...
'Position',[100 100 420 320]);
handles = struct();
%% CONTINUE TO CREATE THE LAYOUT (Button, Extract button, Listbox, Start/End pop-ups, etc) %%
handles.fullpath = {}; % absolute file names
handles.results = {}; % output of user processing
guidata(hFig,handles); % store in figure
function onLoad(~,~)
handles = guidata(hFig);
% Let the user pick *.mat files
[fn, pth] = uigetfile('*.mat',...
'Select one or more MAT-files','MultiSelect','on');
if isequal(fn,0), return, end
if ischar(fn), fn = {fn}; end
handles.fullpath = fullfile(pth,fn);
set(handles.listFiles,'String',fn,'Value',1);
N = numel(fn);
rangeStr = arrayfun(@num2str,1:N,'UniformOutput',false);
set(handles.popupStart,'String',rangeStr,'Value',1,'Enable','on');
set(handles.popupEnd, 'String',rangeStr,'Value',N,'Enable','on');
set(handles.btnExtract,'Enable','on');
guidata(hFig,handles);
end
function onExtract(~,~)
handles = guidata(hFig);
sIdx = handles.popupStart.Value;
eIdx = handles.popupEnd.Value;
if sIdx > eIdx
errordlg('Start index must not exceed end index.','Range error');
return
end
chosen = handles.fullpath(sIdx:eIdx);
results = cell(size(chosen));
hWait = waitbar(0,'Processing files…');
for k = 1:numel(chosen)
S = load(chosen{k});
results{k}= S;
waitbar(k/numel(chosen),hWait);
end
close(hWait);
handles.results = results;
guidata(hFig,handles);
msgbox(sprintf('Done! Processed %d file(s).',numel(chosen)),...
'Success');
end
end
Hope this helps!