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

7 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 26 Sep 2014
Modifié(e) : Geoff Hayes le 26 Sep 2014
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 commentaires
Nicole
Nicole le 27 Sep 2014
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.
Geoff Hayes
Geoff Hayes le 28 Sep 2014
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by