Matrix dimensions must agree
Show older comments
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)
Jan
on 18 Apr 2017
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
Michael
on 18 Apr 2017
Michael
on 18 Apr 2017
Jan
on 19 Apr 2017
@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)
Michael
on 19 Apr 2017
the cyclist
on 18 Apr 2017
0 votes
Have you used debug mode to halt the code, and verify that freq and prescaleleval are really 1x1 as you expect?
5 Comments
Michael
on 18 Apr 2017
the cyclist
on 19 Apr 2017
Edited: the cyclist
on 19 Apr 2017
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.
Or even better, use str2double (which gives a nice NaN for non-number strings, and avoids the whole eval thing)
Michael
on 19 Apr 2017
the cyclist
on 19 Apr 2017
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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!