Accessing appdesigner objects properties from a function

I am trying to create a basic calculator with every operator button calling the same function .I am able to do this with the button label as parameter and a switch case inside the called function to check the prameter and proceed accordingly. How can I improve the app in following terms:
  1. Not pass the label of operator button everytime. Can the button which was pushed be identified without passing the button label? Identifying the operator which is the button label is the goal here.
  2. Have a common buttonPushed callback for multiple buttons since they all call the same function.
  3. Possibly improve how i append the history cell.
properties (Access = private)
history = {'History'}; % Description
end
methods (Access = private)
function func(app,operator)
switch operator
case '+'
app.ResultEditField.Value = app.Number1EditField.Value+app.Number2EditField.Value;
case '-'
app.ResultEditField.Value = app.Number1EditField.Value-app.Number2EditField.Value;
case '*'
app.ResultEditField.Value = app.Number1EditField.Value*app.Number2EditField.Value;
case '/'
app.ResultEditField.Value = app.Number1EditField.Value/app.Number2EditField.Value;
end
a=sprintf('\n %d %c %d = %d',app.Number1EditField.Value,operator,app.Number2EditField.Value,app.ResultEditField.Value );
app.history(end+1)={char(a)};
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: addButton
function addButtonPushed(app, event)
app.func(app.addButton.Text);
end

Answers (1)

First, I'm not sure why you would want to just have a single button pushed callback for multiple buttons.
Why not just break that function up and have the appropriate line of code in the button pushed callback for each of the individual buttons. So for example have the button pushed callback for the plus button have just the line:
app.ResultEditField.Value = app.Number1EditField.Value+app.Number2EditField.Value;
Assuming you really want to have a common button pushed callback with the switch case as you show, you could have each individual button have a button pressed callback that called the common function with the operator argument set appropriately.
So for example the button pushed call back for plus would have the line of code:
func('+')

5 Comments

Thank you for the easiest approach.
The whole point of what I am trying to do is avoid the switch case and reduce the code lines. I would not even need the switch case if I used the basic arithmetic line that you used. Isn't that the first thing that one would do?
I already have the operator (button label) passed as parameter if you see the addbuttonPushed callback. All other buttons have a similar callback function which I have not shared above.
When any button is pressed, I think the trigger and the associated object is stored somewhere. What I am trying to do is see if I can access that when the function func is called.
Further I am trying to learn if the buttonPushed callback of multiple buttons can be combined since they call the same function. This is to avoid function call on individual callbacks.
I don't know if there is any advantage, but instead of sprintf you could use:
a = app.Number1EditField.Value + operator + app.Number2EditField.Value +...
" = " + app.ResultEditField.Value
which uses the ability of the + operator to convert values to strings https://www.mathworks.com/help/matlab/matlab_prog/represent-text-with-character-and-string-arrays.html
I'm sorry but I'm not understanding your comment. Are you saying that my first approach (have a button pressed callback for each operation and perform the appropriate operation there) solve your problem, or is there still something else that you are trying to do?
By the way, if there is some specific code that depends upon which button is pushed and some other code that is done for every button push then you could use the following approach. Put the specific part in the button's button pushed callback. Put the common code in another function. Have each specific button pressed callback call first perform its specific operation and then call the common function
No, the initial approach does not solve what I am trying to learn. And what you wrote about calling the same function is exactly what I did above. But in this case, there is no other function of the buttons other than all calling the same function. This is the reason why I am trying to find a way to combine it.
Further, your suggestion to use the + operator generates a string instead of a character array that I could add later to the history cell which I use with a button to display as a text for a label field. What do you think would be the best way to save lines of character array or strings (i cannot figure out which one is better here) and then display them later? I hope you get the idea of me trying to display the calculator history with a history button here.
I'm sorry. I'm not really understanding then what it is that you are trying to do but are unable to do.

Sign in to comment.

Categories

Products

Release

R2020b

Asked:

on 20 Jan 2022

Commented:

Jon
on 21 Jan 2022

Community Treasure Hunt

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

Start Hunting!