Matlab GUI unit tests, how to call private callback functions
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a complex GUI with a lot of parameters, each of which trigger a callback function. The callback function is private.
Then I am running Matlabs unit tests to change the value of a spinner, and ideally changing this value would trigger the callback and internally call this private function. However this does not happen by default when one remotely changes this spinner value, and as such it seems there is no way to trigger it's callback.
testCase.type(spinnerHandle,100) % this sets my spinner value
But then how to trigger the callback function?
Thanks
1 commentaire
Ayush Aniket
le 21 Déc 2023
Hi Peter,
Can you share your code? I will try to replicate it on my end. It will help me answer the question. You can also share the relevant portion with respect to the "spinner" component.
Réponses (1)
Gowtham
le 27 Déc 2023
Hello Peter,
I understand that you are trying to trigger a private callback function using unit tests.
This can be achieved by manually invoking the specific callback function after changing the value.
I suggest you follow the below steps to understand a sample demonstration:
- Start with creating an app in MATLAB App Designer.
- Add a Spinner component to test.
- Add a UI Button to use it as a trigger.
For a sample demonstration of the above process, kindly refer to the following code snippet:
% Value changed function: Spinner
function SpinnerValueChanged(app, event)
value = app.Spinner.Value;
% Print the value to verify the trigger
disp(value);
end
% Button pushed function: Button
function ButtonPushed(app, event)
% Test trigger
% Udpate the spinner value
spinnerHandle = app.Spinner;
spinnerHandle.Value = 100;
% Capture the callback function
callbackFcn = spinnerHandle.ValueChangedFcn;
% Check if the callback is specified and if it is a function handle
if ~isempty(callbackFcn)
if isa(callbackFcn, 'function_handle')
% Call the callback function directly
callbackFcn(spinnerHandle, event);
end
end
end
Below is the UI of the app and the expected output of the demonstration:
Kindly follow the below steps for testing:
- Increment the Spinner value
- Increment the Spinner value
- Increment the Spinner value
- Click the Button to trigger the callback internally
It appears you are looking to interact with a private method, and one approach might be to access it through an interface, specifically a public method of the same class that acts as a helper function.
Kindly refer to the following MathWorks Documentation(s) for further information on how to use the mentioned methods:
- MATLAB App Designer: https://www.mathworks.com/help/matlab/creating_guis/create-a-simple-app-or-gui-using-app-designer.html
- UI Components: https://www.mathworks.com/help/matlab/creating_guis/choose-components-for-your-app-designer-app.html
- Callbacks in App Designer: https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html
Hope this helps.
Best Regards,
Gowtham
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!