필터 지우기
필터 지우기

How to display names in edit field of the files selected to run .m file using button in appdesigner

조회 수: 4 (최근 30일)
Hi I have a .m file which I wants to run using button function.
The .m file asks for the two input .mat files to be selected to execute
How can I display the names of the two input .mat files selected in the edit field boxes.
%%%%%%%% main model for reference
function RunModel
MFilDirName='ModelScripts'; %% Sub directory contains the supporting .m files/functions
addpath(MFilDirName) %% Add this subdirectory to the Matlab search path
if strcmp(LoadFiles(2),'EndExit') %% Load file is a .m function inside ModelScripts
disp('User abort')
return
end
end
%%%%%%%%%%%%%%% App designer run button
function RunButton(app, event)
RunModel;
end

답변 (1개)

Deepak
Deepak 2024년 8월 8일
Hi Harish,
To my understanding, you have a MATLAB script that takes as input two MAT files, and you want to run the MATLAB script with a Push Button callback and display those two MAT file names in the Edit Field of the App Designer.
To solve this task, we can create a “LoadFiles.m” MATLAB script that will open a dialog box to select two MAT Files, then get the file names from the entire path by using the “fileparts” function of MATLAB.
In our Push Button Callback (RunButton), we can set the values of both Edit Fields with the file names if the status is correct. This way, we can display both MAT file names in the Edit Field.
Below is the App Designer and MATLAB code that addresses the task:
MyApp.mlapp (RunButton Callback)
function RunButton(app, event)
[status, fileNames] = RunModel;
if strcmp(status, 'Continue')
app.EditField1.Value = fileNames{1};
app.EditField2.Value = fileNames{2};
else
disp('User abort');
end
end
RunModel.m
function [status, fileNames] = RunModel
MFilDirName = 'ModelScripts'; %% Subdirectory contains the supporting .m files/functions
addpath(MFilDirName); %% Add this subdirectory to the Matlab search path
[status, fileNames] = LoadFiles(2); %% Load file is a .m function inside ModelScripts
if strcmp(status, 'EndExit')
disp('User abort');
return;
end
end
LoadFiles.m
function [status, fileNames] = LoadFiles(numFiles)
fileNames = cell(1, numFiles);
for i = 1:numFiles
[file, ~] = uigetfile('*.mat', 'Select a MAT-file');
if isequal(file, 0)
status = 'EndExit';
return;
else
[~, name, ext] = fileparts(file);
fileNames{i} = [name, ext];
end
end
status = 'Continue';
end
Attaching the documentation of functions used in the MATLAB script for reference:
I hope this helps.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by