Filter löschen
Filter löschen

Matlab GUI unit tests, how to call private callback functions

7 Ansichten (letzte 30 Tage)
Peter McCutcheon
Peter McCutcheon am 15 Dez. 2023
Beantwortet: Gowtham am 27 Dez. 2023
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 Kommentar
Ayush Aniket
Ayush Aniket am 21 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Gowtham
Gowtham am 27 Dez. 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:
  1. Start with creating an app in MATLAB App Designer.
  2. Add a Spinner component to test.
  3. 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:
  1. Increment the Spinner value
  2. Increment the Spinner value
  3. Increment the Spinner value
  4. 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:
Hope this helps.
Best Regards,
Gowtham

Kategorien

Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by