I'm getting an error in my function I don't understand
1 view (last 30 days)
Show older comments
My function is as follows
function [I,V] = MeasureResistance(R0,N,Imin,Imax,sigmaI,typeV)
if nargin < 2
N = 1;
I0 = -1 + 2*rand(N,1);
Ve = randn(N,1);
Ie = randn(N,1);
elseif nargin < 3
I0 = -1 + 2*rand(N,1);
Ve = randn(N,1);
Ie = randn(N,1);
elseif nargin < 7
I0 = Imin + (Imax - Imin)*rand(N,100000);
Ie = sigmaI;
if typeV == 'N'
Ve = randn(N,1);
elseif typeV == 'U'
Ve = rand(N,1);
end
end
V0 = R0*I0;
I = I0 + Ie;
V = V0 + Ve;
However when I run the function I get this error
Error: File: MeasureResistance.m Line: 18 Column: 17 The expression to the left of the equals sign is not a valid target for an assignment.
Any insight as to what may be causing this?
2 Comments
Image Analyst
on 14 Oct 2014
Like almost all other people, you didn't copy the full error message - all the red text. You just snipped out a part and left out the crucial part of what line 18 is. What is line 18? The error message will tell you. Is it
Ve = rand(N,1);
because that's what I get when I count down your code. If so, there's nothing wrong with that line.
Accepted Answer
per isakson
on 14 Oct 2014
Edited: per isakson
on 14 Oct 2014
To add two arrays they must have the same size. Obviously, with N=2 V0 and Ve don't have the same size. That's what the error message says.
Set
dbstop if error
and run the function with N=2. Execution will stop at line 25. Inspect V0 and Ve and you will see which size differs from your expectation.
 
The debugging tools of Matlab are good! Here are some links on debugging in Matlab
0 Comments
More Answers (1)
Guillaume
on 14 Oct 2014
The best way for you to find out what is going wrong is to type:
dbstop if error
at the command line before calling your function. When the error occurs, it will go into debug mode and you can inspect V0 and Ve to see what their size is.
Additionally, you could also set a breakpoint at the start of your function and then step through your code line by line to see where it is going wrong.
0 Comments
See Also
Categories
Find more on Logical 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!