Clear Filters
Clear Filters

Undefined Function or Variable

2 views (last 30 days)
Maddie Sanders
Maddie Sanders on 18 Apr 2020
Commented: Tommy on 19 Apr 2020
I am trying to create a function at the end of my script. I believe in my function I defined the variable ‘u.’ But then later down in the function when I try to use ‘u’ I get an error saying that ‘u’ is not defined. I am attaching an image with my code and the error. Let me know if anyone has any suggestions. Any help is appreciated.
For some reason I am having trouble submitting multiple images but I have two ‘end’s at the end of my code. Also the error message is in line 272 ‘if u==1’

Accepted Answer

Tommy
Tommy on 18 Apr 2020
length(o) and length(n) will both be 1, as o and n are scalars, so your code never enters your for loops. You should instead use just o and n in their place. i.e.
for W = 1:o-1
  5 Comments
Maddie Sanders
Maddie Sanders on 19 Apr 2020
Thank you so much for your help. It is very much appreciated. I was having issues with being able to ask questions on my computer so I had to do it on my phone which doesn't have access to my code, so I apologize for the inconvenience and thank you again for your help.
I do have one more question because my code isn't doing what I want it to.
V2 is a vector that is 51X1000. I want to use the conclusion function on this vector. In each row of the vector the values get larger. Ex: (1,1)=50 and (2,1)=60. Through my function I am trying to show this consistent relationship that as you go down in rows, the values get larger, but my code is getting these results so I know I am doing something wrong.
I am trying to code for all the columns in the vector V2 if row 1 is greater than row 2 display a 1 in vector u, if row 2>row 3, display a 1, and so on.
If row 1<row 2 display a 2, if row 2<row 3 display a 2...
I don't know if maybe in my u(W)=1/2/0 if I need to do u(W,K) or something like that.
I am just confused what my function is actually doing because when I display 'u' the values are nothing like what I predicted (which would be all 2s).
function [message] = conclusion(A)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
[o,n]=size(A);
for W=1:o-1
for K=1:n
if abs(A(W,K))>abs(A(W+1,K))
u(W)=1;
elseif abs(A(W,K))<abs(A(W+1,K))
u(W)=2;
else
u(W)=0;
end
end
end
if u==0
message=disp('The results are inconconclusive.');
elseif u==1
y=input('Is this graph showing multiple values of length of the distributed load or multiples values of the force per meter value of the distributed load? Say either length or force.');
message=disp('As the value of the length of the distributed load increases, the internal load decreases.');
elseif u==2
y=input('Is this graph showing multiple values of length of the distributed load or multiples values of the force per meter value of the distributed load? Say either length or force.');
message=disp('As the value of the length of the distributed load increases, the internal load decreases.');
else
message='The results are inconconclusive.'
disp(num2str(u))
end
end
Tommy
Tommy on 19 Apr 2020
Always happy to help! Don't sweat it.
"I don't know if maybe in my u(W)=1/2/0 if I need to do u(W,K) or something like that."
If you do want to test this for all columns, then you would need a second index. Otherwise, the results of each column are being overwritten by the results of the next column. Check if u contains the expected output for the very last column in A - I believe that should be the case...
Also, are any values within A negative?
If I am understanding correctly, the point of this function is to tell the user whether values in A increase or decrease as you move down rows? Can one column increase while another column decreases?
You could simplify the process by using diff and sign. Consider these examples:
>> A = [10 20 30;40 50 60;70 80 90]
A =
10 20 30
40 50 60
70 80 90
>> sign(diff(A))
ans =
1 1 1
1 1 1
>> A = [70 80 90;40 50 60;10 20 30]
A =
70 80 90
40 50 60
10 20 30
>> sign(diff(A))
ans =
-1 -1 -1
-1 -1 -1
>> A = [10 20 30;70 80 90;40 50 60]
A =
10 20 30
70 80 90
40 50 60
>> sign(diff(A))
ans =
1 1 1
-1 -1 -1
>> A = [10 80 60;70 50 90;40 20 30]
A =
10 80 60
70 50 90
40 20 30
>> sign(diff(A))
ans =
1 -1 1
-1 -1 -1
Then it seems to me that you'd want to check if every row is increasing across every column. You might then use all. Am I on the right track?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!