I'm getting an error in my function I don't understand

1 view (last 30 days)
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
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.
Obsidian
Obsidian on 14 Oct 2014
Edited: per isakson on 14 Oct 2014
Sorry about that I'm still new to the forums.
Either way I'm not getting the error message anymore for that particular issue. I am now receiving another error.
The code is now
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);
if typeV == 'N';
Ve = randn(N,100000);
elseif typeV == 'U';
Ve = rand(N,100000);
end
Ie = sigmaI;
end
V0 = R0*I0;
I = I0 + Ie;
V = V0 + Ve;
When I run the function with N = 1 I have no issues, however once I bumped it to N = 2 I get the error
"Error using + Matrix dimensions must agree. Error in MeasureResistance (line 25) V = V0 + Ve;"
I have to take Measurements with N = 1,2,4,8. I don't understand why it suddenly doesn't work. I did the math outside of the function step by step and it works, I'm almost sure the matrix dimensions are correct.

Sign in to comment.

Accepted Answer

per isakson
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.
&nbsp
The debugging tools of Matlab are good! Here are some links on debugging in Matlab

More Answers (1)

Guillaume
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.

Community Treasure Hunt

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

Start Hunting!