Matrix dimensions must agree

So I have a question regarding this error that I'm seeing.
Error using /
Matrix dimensions must agree.
here is the code I'm running
function [] = setPWMFreq(obj, freq)
prescaleleval = 25000000
prescaleleval = prescaleleval / 4096
prescaleleval = prescaleleval / freq;
I'm getting the erro on the third line
prescaleleval = prescaleleval / freq;
freq = 100
prescaleleval = 25000000
both variables are 1x1, so I don't see how I'm getting a matrix error.
I attempted to convert freq to a double via double(freq) and I get
Error using /
Matrix dimensions must agree.

Answers (2)

The problem looks strange, but Matlab does not do any magic things. Therefore there must be a deterministic reason for the error message. It might get clear, if you post the complete message, which should contain the failing line also.
What happesn, if you run the code by copy&paste n the command line? Perhaps the shown code is not the code, which runs on your computer?
Use the debugger for further investigations. Type this in the command line and start the code again:
dbstop if error
Where does Matlab stop then? What are the values and types of the variables used in the failing command?

4 Comments

Thank you for helping out.
here is the full response. I removed the semicolons and inserted freq so that it would show the exact values being used.
>> rpi = raspi('192.168.1.13');
>> Step
prescaleleval =
25000000
prescaleleval =
6.1035e+03
freq =
100
Error using /
Matrix dimensions must agree.
Error in PWM/setPWMFreq (line 57)
prescaleleval = prescaleleval / freq;
Error in MotorHat (line 23)
motor_obj.pwm.setPWMFreq(motor_obj.frequency);
Error in Step (line 1)
step = MotorHat(rpi, '60', '100');
and I did dbstop if error and it stops right on
prescaleleval = prescaleleval / freq;
@Michael: Please read my answer carefully. If Matlab stops at the error: "What are the values and types of the variables used in the failing command?"
class(prescaleleval)
size(prescaleleval)
class(freq)
size(freq)
ooooo thanks for that. like I said I'm a bit knew with matlab. I see what you were getting at. So I found the problem.
prescaleleval was being valued as a 1x1 double, but freq was actually showing up as a 1x3 char array. So I used "the cyclists" suggestion and used str2double. and that fixed it.
again thanks for your help!! I'm sure I'll be back for more. but I now know to look at a different aspect when I'm debug, so thats great. Thank you.

Sign in to comment.

the cyclist
the cyclist on 18 Apr 2017
Have you used debug mode to halt the code, and verify that freq and prescaleleval are really 1x1 as you expect?

5 Comments

Thanks for getting back so fast.
I have used the debugger. I stop it right before I'm getting the error and step through keeping an eye on the variables. it looks fine to me. Which is why I'm at a lose. I re-ran the code and took off the semicolon and inserted freq to show you the exact numbers being used.
>> rpi = raspi('192.168.1.13');
>> Step
prescaleleval =
25000000
prescaleleval =
6.1035e+03
freq =
100
Error using /
Matrix dimensions must agree.
Error in PWM/setPWMFreq (line 57)
prescaleleval = prescaleleval / freq;
Error in MotorHat (line 23)
motor_obj.pwm.setPWMFreq(motor_obj.frequency);
Error in Step (line 1)
step = MotorHat(rpi, '60', '100');
Uh oh! Looks like your inputs might actually be character arrays, not numerics. So, I'm guessing you are doing
'60'/'100'
or some such. That will the the error you are seeing. Either change those to actual numbers at the input, or convert using str2num at some point along the way.
Stephen23
Stephen23 on 19 Apr 2017
Edited: Stephen23 on 19 Apr 2017
Or even better, use str2double (which gives a nice NaN for non-number strings, and avoids the whole eval thing)
got it. it seems freq was not a 1x1, but a 1x3 char. I went deeper into the debugging support and saw the issue. thanks for the help.
The best form of thanks is accepting and/or upvoting the most helpful answers. This rewards the contributor, and can direct future users toward those answers.

Sign in to comment.

Asked:

on 18 Apr 2017

Edited:

on 19 Apr 2017

Community Treasure Hunt

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

Start Hunting!