Accessing appdesigner objects properties from a function
조회 수: 3 (최근 30일)
이전 댓글 표시
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:
- 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.
- Have a common buttonPushed callback for multiple buttons since they all call the same function.
- 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
댓글 수: 0
답변 (1개)
Jon
2022년 1월 20일
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
Jon
2022년 1월 21일
I'm sorry. I'm not really understanding then what it is that you are trying to do but are unable to do.
참고 항목
카테고리
Help Center 및 File Exchange에서 Platform and License에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!