Array indices must be positive matlab [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 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)))

Related

Matlab - plot data from array of structs [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an array of structs. Every struct holds a set of data for one single measure. Matlab gives me an error when I try to plot this data.
Expected one output from a curly brace or dot indexing expression, but there were 361 results.
How should I rewrite my plot code?
plot(result.structArray_A(:).nonArrayValue_X, result.structArray_A(:).nonArrayValue_Y);
I have found a solution. Simply writing the following does work, even if the syntax does look odd:
plot([result.structArray_A.nonArrayValue_X], [result.structArray_A.nonArrayValue_Y]);

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

Failing show legend [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 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.

MATLAB plotting syntax error? [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 watched a video titled "Writing a matlab program" and tried to use the syntax exactly, yet I still can't see how I've made an error...can you please help?
My code is simply
x= [-10:0.1:10];
y=2x^2-x + 1;
plot (x,y)
I'm getting a parse error at x and I have no idea what that means..
Matlab cannot parse 2x. Multiplication requires the * symbol. Either * or .* will work in this case.
Your next problem will be the use of ^ instead of .^ —this time you're going to have to include the dot. Google for "elementwise" operators in matlab to understand what difference the dot makes.

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! :(