Trigger a function inside a running GUI from cmd or powershell without reopening it
3 次查看(过去 30 天)
显示 更早的评论
Hello, I want to trigger or call a function inside my GUI, so it can do some tasks. The GUI is already running, and I don't to open a new one. While triggering the function I also want to send some instruction to the function as text or number, so it can conduct a particular process. Is it possible?
0 个评论
回答(1 个)
Aditya
2023-12-22
Hi Mubin,
I understand that you are looking to invoke functions within a MATLAB GUI from an external command line tool. This process is inherently complex due to the separation between the GUI and command-line environments. However, you do have the option to utilize MATLAB's command line interface to call the necessary function handles as outlined.
For clarity, here is a step-by-step example that you can follow to implement such functionality within your GUI code:
% Inside your GUI code
function myGUI
% Define a global variable
global commandToRun;
% Initialize the global variable
commandToRun = '';
% Set up a timer to periodically check for commands
t = timer('ExecutionMode', 'fixedRate', 'Period', 1, 'TimerFcn', @checkCommand);
start(t);
% Rest of your GUI setup code...
end
% Timer callback function
function checkCommand(~, ~)
global commandToRun;
if ~isempty(commandToRun)
% Execute the function with the command
executeFunction(commandToRun);
% Reset the command
commandToRun = '';
end
end
% Function to execute commands
function executeFunction(command)
% Your function code here, using 'command' as input
disp(['Executing command: ', command]);
% ... (function code)
end
To trigger the function within your GUI from MATLAB's command line or from within a script, you would set the global variable as follows:
% From MATLAB command line or a script
global commandToRun;
commandToRun = 'instruction1’; % This could be any text or number
For additional information on the use of global variables in MATLAB, please consult the following documentation:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!