Import multiple .mat files into GUi

3 views (last 30 days)
JEONGSOO BAE
JEONGSOO BAE on 14 Aug 2017
Answered: TED MOSBY on 4 Jul 2025
Hello I am writing code for gui which will load multiple .mat files, by using uigetfile then process it as following.
  1. 'Load' button will open up dialog which allows user to choose which files will be processed.(Done)
  2. It will display the loaded files in 'File List' listbox.(Done)
  3. Choosing the range of loaded files with 'pop-up' menu.(Files are numbered as same format. e.g.) file1, file2, file3...)
  4. ' Extract' will be choosing range that user chose, then do the process(already have the code for the process, but I don't know how to choose those specific files to be processed)
The bolded lines are my main question is.
Thanks!

Answers (1)

TED MOSBY
TED MOSBY on 4 Jul 2025
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!

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!