A nice MATLAB App Designer question!
Here's a step-by-step guide to create a simple MATLAB App that:
- Has a Text Box (also known as
EditField) to input a folder name.
- Has a Load push button to trigger the loading of
.mat files from the specified folder.
- Loads the
.mat files from the folder and stores them in the App's workspace (or base workspace, your choice).
Create a new App in MATLAB App Designer
- Open MATLAB and type
appdesigner in the Command Window. This will launch the App Designer.
- Click on New App and choose Blank App.
- Save your App as
LoadMatFilesApp.mlapp (or any name you prefer).
Design the UI
Drag and drop the following components from the Component Library (left sidebar) onto the Canvas (center area):
- Label (rename it to
FolderNameLabel):
- Text:
Enter Folder Name:
- Position: (20, 20, 120, 22) (adjust as needed)
- Edit Field (Text) (rename it to
FolderNameEditField):
- Position: (150, 20, 200, 22)
- This will be our Text Box for inputting the folder name.
- Button (rename it to
LoadButton):
- Text:
Load
- Position: (360, 20, 60, 22)
- This will be our Load push button.
- Text Area (rename it to
StatusTextArea):
- Position: (20, 50, 400, 100)
- This will display status messages (optional, but nice to have).
Your UI should look like this:
Code the callbacks
Now, let's write the code to make our App functional. In the Code View (button at the top-right corner), add the following code:
% App properties (for storing loaded data)
properties (Access = private)
LoadedData % Store loaded .mat files here
end
% Button pushed function: LoadButton
function LoadButtonPushed(app, event)
% Get the folder name from the EditField
folderName = app.FolderNameEditField.Value;
% Check if folder name is not empty
if isempty(folderName)
app.StatusTextArea.Value = 'Error: Please enter a folder name!';
return;
end
% Check if folder exists
if ~exist(folderName, 'dir')
app.StatusTextArea.Value = sprintf('Error: Folder "%s" does not exist!', folderName);
return;
end
% Get a list of .mat files in the folder
matFiles = dir(fullfile(folderName, '*.mat'));
numFiles = numel(matFiles);
% Preallocate a struct to store loaded data
app.LoadedData = struct();
% Loop through each .mat file and load it
for i = 1:numFiles
fileName = matFiles(i).name;
filePath = fullfile(folderName, fileName);
varName = strrep(fileName(1:end-4), '.', '_'); % clean var name
% Load the .mat file into a struct field
tmpData = load(filePath);
app.LoadedData.(varName) = tmpData;
% Display status message
app.StatusTextArea.Value = sprintf('Loaded %d/%d: %s', i, numFiles, fileName);
pause(0.1); % brief delay for UI update
end
% Final status message
app.StatusTextArea.Value = sprintf('Loaded %d .mat files from "%s"', numFiles, folderName);
% Optional: assign loaded data to base workspace (uncomment if needed)
% assignin('base', 'LoadedData', app.LoadedData);
end
Here's what the code does:
- The
LoadButtonPushed function is triggered when the Load button is clicked.
- It retrieves the folder name from the
FolderNameEditField.
- Checks if the folder exists and is not empty.
- Uses
dir to get a list of .mat files in the specified folder.
- Loops through each
.mat file, loads it using load, and stores the data in a dynamic struct field app.LoadedData.
- Updates the
StatusTextArea with progress messages.
- Optionally (uncomment the last line), assigns the
LoadedData struct to the base workspace (if you want to access it outside the App).
Run your App!
Click the Run button (or press F5) to launch your App. Enter a valid folder name containing .mat files, and click the Load button.
The App will load the .mat files and display status messages. Once finished, you can inspect the app.LoadedData struct in the App's workspace (use the Workspace browser in App Designer).
If you uncommented the assignin line, you'll also find LoadedData in your base workspace.
That's it! You've successfully created a MATLAB App to load .mat files from a user-specified folder.
Happy App-building!