Getting error matrix dimensions must agree when I run a stack that calculates the sin of the last number on the stack.

i have a rpmcalcuator stack that only takes certain operands. sin is one of them. when i try to add sin to my stack, it gives me an error.
i am attaching both files. stackmain runs rpm calc. if you could help me figure out how to code my sin function i would appreciate it.

 채택된 답변

Nicole - a helpful statement to run before executing your code is dbstop if error. This will cause the code to pause at the line that is generating the error message. In your case, the error occurs at
elseif (x == 'pi')
because you are comparing a string of length three (for sin) versus a string of length two (for pi).
When doing string comparisons, you should use strcmpi. So your above line of code now becomes
elseif strcmpi(x,'pi')
Do the same for the comparison with 'e' and try running through the code again.
EDIT
Once you fix the above, you will notice an error at line 94 of RPMCalculator
self.push(num2str(sin(self.data(self.top-1),u)));
because the variable u is undefined. Since sin only takes one input, you can remove this.

댓글 수: 6

if you don't mind, i do have another question. i forgot to ask.
The matlab screen prints out stuff I don't want it to but I don't know how to stop it. i have ; everywhere I don't want it to print but it continues to print out
/
ans =
RPMCalculator with no properties.
sin
ans =
RPMCalculator with no properties.
How do i stop that?
Typically, the unexpected output occurs (especially when written to the Command Window as ans) occurs on lines of code that aren't terminated with a semi-colon. In this case, one is needed at the end of line 44 (in RPMCalculator.m)
else
self.math(x); % <--- semi-colon needed here
end
The +, sin, and / occur because of the disp at line 83 in the math function
function [self] = math(self, x)
disp(x)
switch x
I put ; in more places and it suppressed the
ans =
RPMCalculator with no properties.
It is still printing out my case strings. if i put a = or + it will show that. I put ; after each case argument. is it the way i am calling it out? it's not the worst thing in the world but it's bugging me.
Hi Nicole - I think it is because of the disp(x) in the math function that the + or sin or - is appearing. You shouldn't need to put semi-colons after the case argument. As soon as I commented out the disp(x) line, the only output in the Command Window (when running your original StackMain is
d (i) =
12
The last 3 values on the stack are:
4
3
12

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 9월 26일

댓글:

2014년 9월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by