Access inputs from app designer in the MATLAB script.

48 views (last 30 days)
I'm currently trying to make an app using App Designer in MATLAB, and I'm facing a challenge with integrating user input from one of the tabs into my MATLAB script.
I aim to gather user input, store it in the base workspace, and then utilize these inputs for calculations within my MATLAB script.Ultimately, I intend to display the calculated results back to the user within the corresponding tab of my app.
For example lets consider something like this
The user adjusts slider values, clicks a button, and expects to see the calculated results in the output section of the app.
Any help or suggestions would be greatly appreciated.

Accepted Answer

Pavan Sahith
Pavan Sahith on 19 Apr 2024
Edited: Pavan Sahith on 29 Apr 2024
I see that you are trying to achieve seamless integration of user input from your App Designer application into your MATLAB script.
You can do that by defining the Button Callback Function in App Designer. You can refer to this code snippet to get started :
  • This function will capture user inputs from sliders, store these inputs in the 'base workspace', invoke an external MATLAB script to perform calculations, and finally, display the calculated results back in the app.
function ButtonPushed(app, event)
% Retrieve slider values
a = app.Slider.Value;
b = app.Slider2.Value;
% Assign these values to the base workspace
assignin('base','a_value',a);
assignin('base','b_value',b);
% Call the external MATLAB script that performs the calculation
MATLABScript; % Ensure this script is on the MATLAB path or current folder
% Retrieve the calculated result from the base workspace
c = evalin('base', 'c');
app.OutputLabel.Text = num2str(c);
end
  • Here's an example script that demonstrates how to access the stored inputs, carry out a calculation (in this example, multiplication), and then save the result back to the 'base workspace':
a_value = evalin('base', 'a_value');
b_value = evalin('base', 'b_value');
% Perform the calculation
c = a_value * b_value;
% Assign the result back to the base workspace
assignin('base', 'c', c);
  • This method uses the ‘assignin’ and ‘evalin’ functions to facilitate communication between your app and the MATLAB script, ensuring that user inputs are effectively utilized and the results are accurately displayed within your app.
You can refer to the following MathWorks documentation to know more about the 'assignin' and 'evalin'
Hope this information helps you in getting started.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer 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!