o4-mini-2025-04-16· OpenAI LMArena Human Preference 140K
enter folder name in textbox and load the .mat files in the folder with the load push…
- User
enter folder name in textbox and load the .mat files in the folder with the load push button on matlab app
- Assistant
Here’s a minimal example of how you can build a simple App Designer app with:
- An edit-field where the user types (or pastes) a folder path.
- A “Load” button that finds all
.matfiles in that folder and loads them into the MATLAB base workspace.
1) Design the UI
- Open App Designer (
>> appdesigner) - Drag in:
- Edit Field (Text) → rename its
ComponentNametoFolderEditFieldand itsLabelto “Folder:” - Button → rename its
ComponentNametoLoadButtonand itsTextto “Load .mat Files”
- Edit Field (Text) → rename its
Your Component Browser should look roughly like:
MyApp (UIFigure) ├─ FolderEditField (EditField) └─ LoadButton (Button)
2) Add the Button Callback
Select the LoadButton, go to the Code Browser → Callbacks → ButtonPushedFcn, and implement:
% Button pushed function: LoadButton function LoadButtonPushed(app, event) % 1) Get folder from edit field folderPath = app.FolderEditField.Value; % 2) Validate if ~isfolder(folderPath) uialert(app.UIFigure, ... 'The folder path is invalid. Please check and try again.', ... 'Invalid Folder'); return end % 3) Find all .mat files matFiles = dir(fullfile(folderPath, '*.mat')); if isempty(matFiles) uialert(app.UIFigure, ... 'No .mat files found in this folder.', ... 'No Files'); return end % 4) Load each .mat into the base workspace for k = 1:numel(matFiles) fileName = matFiles(k).name; fullFile = fullfile(folderPath, fileName); % Load into a struct S = load(fullFile); % Create a valid variable name from the filename [~, varName] = fileparts(fileName); varName = matlab.lang.makeValidName(varName); % Assign to base workspace assignin('base', varName, S); end % 5) Confirmation uialert(app.UIFigure, ... sprintf('Loaded %d .mat file(s) into the base workspace.', numel(matFiles)), ... 'Success'); endWhat this does
- Reads the folder path from
app.FolderEditField.Value. - Checks that the folder exists.
- Lists all
*.matfiles withdir. - Loads each file into a temporary struct
S. - Assigns that struct to a variable in the base workspace whose name matches the file (sans “.mat”), sanitized with
makeValidName. - Pops up a small confirmation or error with
uialert.
3) Run & Test
- Click Run in App Designer.
- In the running app:
- Enter (or browse–paste) a folder path into “Folder:”
- Click Load .mat Files
- Switch to the MATLAB Command Window and type
whosto see the newly loaded variables.
That’s it! 🎉
You now have a basic UI for loading entire folders of.matfiles with one click. Feel free to extend it (e.g. add a “Browse…” button, display file list in a table, progress bar, etc.).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.