Failing show legend [closed] - matlab

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a huge amount of data to plot, and thanks to:
x=matlab.lang.makeValidName(strcat(...));
assignin('base',x,I);
for each loop of process, where I calculate I, I assign the value of I (a vector) to the variable name I_Pnum1_Bnum2 where num1 is the value of P and num2 the value of B. So, at the end I have lots of I's for:
num1=-4:-1:-14;
num2=[0 5 10 20:20:120 150 170 200 220];
That's why, for each value of P, I want to plot (on the same graph) all I's for different B:
num1=-4:-1:-14;
num2=[0 5 10 20:20:120 150 170 200 220];
for i=1:length(num1)
legend=[];
figure(i)
for j=1:length(num2)
Y=matlab.lang.makeValidName(strcat('I_p',num2str(abs(num1(i))),'_B',num2str(double(num2(j)))));
plot(V,eval(Y),'linewidth',2)
hold on
leg=strcat("B= ",num2str(b(j)));
legend=[legend leg];
end
title(strcat("Caractéristiques I(V) #",num2str(p(i)),"dBm"))
legend(legend);
end
clc;
The problem: I get
Function 'subsindex' is not defined for values of class 'string'.
and it is due to the line legend(legende), and I don't understand why, because the vector legende is well defined.

The error occurs because of a conflict between your variable named legend and the build-in MATLAB function legend(). Rename your variable to e.g. leg1 then it should work as expected.

Related

Array indices must be positive matlab [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am new to MATLAB, I have created a triangle, I used the code as below to find all coordinates of x,y.
side_1=[linspace(3,5,100);linspace(2,3,100)]
side_2=[linspace(5,4,100);linspace(3,5,100)]
side_3=[linspace(4,3,100);linspace(5,2,100)]
all_coordinates=[side_1,side_2,side_3]
I used the code as below to find angles.
angles=zeros(300,1);
for i=1:300
angles(i)=atan(all_coordinates(2,i)/all_coordinates(1,i))*180/pi
end
Since view point is flatview, y=0, x=angles
I used this code to plot angles.
for i = range(length(angles))
scatter(angles(i),0)
end
Got error array indices must be positive integers or logical values.
Not sure what you are trying to do with the loop but the problem is that you last loop is wrong.
range(length(angles)) =0, you should use for i=1:length(angles) instead
In addition the two loops are unnecessary. Not sure what you are trying to do with the scatter plot, but you can just write:
angles=atan(all_coordinates(2,:)./all_coordinates(1,:))*180/pi
figure
scatter(angles,zeros(1,length(angles)))

For cycle setting from the n-1 position to the 1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm solving a specific matrix following a theoretical code from a book.
My issue is pretty simple I set that my last value from the one column matrix X is equal to the last one of my one column matrix Z.
After that I do for cycle where I need to pick the second to last until the first.
I don't know how to right this in code way
The book has " i = n-1,....,1 "
If it was i = 1, ....., n-1 it would be easy.
I tried the i=(n-1):1
And this i=(n-1):-1:1
Doesn't work. Returns has empty
To be clear to code is the follow:
i=n-1:1
x(i,1)=z(i,1)-u(i,i+1)*x(i+1,1)
So if n=4, i already have x(4).
So X(3)=z(3)-u(3,4)*x(4,1)
More of the code:
n=4;
Matrix=[2,-1,0,0,1;-1, 2,-1,0,0;0,-1,2,-1,0;0,0,-1,2,1];
u=eye(4,4);
l(1,1)=Matrix(1,1);
u(1,2)=Matrix(1,2)/l(1,1);
z(1,1)=Matrix(1,n+1)/l(1,1);
for i=2:n-1
l(i,i-1)=Matrix(i,i-1);
l(i,i)=Matrix(i,i)-l(i,i-1)*u(i-1,i);
u(i,i+1)=Matrix(i,i+1)/l(i,i);
z(i,1)=(Matrix(i,n+1)-l(i,i-1)*z(i-1,1))/l(i,i);
endfor
l(n,n-1)=Matrix(n,n-1);
l(n,n)=Matrix(n,n)-l(n,n-1)*u(n-1,n)
z(n,1)=(Matrix(n,n+1)-l(n,n-1)*z(n-1,1))/l(n,n)
x(n,1)=z(n,1);
for i=(1-n):-1:1
x(i,1)=z(i,1)-u(i,i+1)*x(i+1,1);
endfor

Error in MATLAB's plot function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this function
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
where thDesnorm is:
[-23.6608
0.1919
0.1866]
When i want to plot this function this way:
domain = 0:1:100;
figure;
plot(domain, h45(domain));
I get this error:
Error using *
Inner matrix dimensions must agree.
Error in #(x)1/(1+exp(-thDesnorm'*[1,45,x]'))
This function works when calling it with, for example, h45(1).
I guess than in plotting, the function is receiving all the domain vector as the parameter x, and not one by one the values of this vector.
As you guessed, the parameter x is your full array domain. This obviously causes a dimensional error in the dot product thDesnorm'*[1,45,x]. A quick fix would be using arrayfun to evaluate h45. For instance:
thDesnorm = [-23.6608
0.1919
0.1866];
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
domain = 0:1:100;
figure;
plot(domain, arrayfun(#(x)h45(x),domain)) % See modification in h45 call

Why is my for loop running over? (Matlab) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the following code:
for i = 1:RGB_size(2) % RGB_size(2) = 1296 //X coords
for j = 1:1:RGB_size(1) % RGB_size(1) = 964 //Y coords
if mask(i,j) == 1
data(next_pixel,:) = [ImgIndex, ImgTake, i, j, RGB(i,j,1), RGB(i,j,2), RGB(i,j,3),...
HSIR(i,j,1), HSIR(i,j,2), HSIR(i,j,3)];
next_pixel = next_pixel + 1; %get next pixel
end
end
end
But Matlab won't run my code because it says I'm trying to access mask(965,1) and my variable mask size is 1296×964. However, I don't see how this is possible. Any thoughts?
RGB_size is calculated from a variable called RGB, which is the same variable used to create the variable mask. I have verified that they are both the same size with the debugger.
Actual error message is:
Attempted to access mask(965,1); index out of bounds because size(mask)=[964,1296].
You have switched the i and j index of the mask.

Why is Matlab creating a second ghostly instance for my variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on a matlab program where at one point I create a 3x1001 matrix called 'bots'. I checked that Matlab created this matrix correctly, but as soon as Matlab enters a while-loop, there is an extra matrix called 'bots' of dimensions 1x1001 with different doubles in it.
...
bots=zeros([3 1001]);
bots(1,:)=botsStartPlace;
bots(2,1:nbBots-1)=botsStartPlace(2:nbBots);
bots(2,nbBots)=length(indexTable);
whos bots % bots 3x1001 24024 double
while(going)
whos bots %bots 3x1001 24024 double
%bots 1x1001 8008 double
....
Anyone has a clue why matlab is doing this? It's freaking me out! :p
The following snippet does not create a "ghostly" instance:
bots = zeros(3, 1001);
whos
going = 2;
while (going)
whos
going = going - 1;
end
The output is:
bots 3x1001 24024 double
going 1x1 8 double
So the problem cannot really come from the while-loop itself.
I have found the problem! My bad, there never was a ghost! Matlab had already gone through the loop once and was ordering the 'whos' output of different calls to the function together which gave me the impression that there where multiple instances. Instead I was just accidentally hurting the matrix in the loop and thereby changing it's dimensions! Sorry for your time! :(