plot3 out of loop

조회 수: 4 (최근 30일)
Oday Shahadh
Oday Shahadh 2020년 6월 9일
댓글: Oday Shahadh 2020년 6월 10일
Can you please fix empty plot in below script
clear
close all
format long
theta=0:10:360;
rho=0:.5:2;
L=-2:0.5:2;
X= cell(3,1) ; Y= cell(3,1);Z = cell(3,1) ;
for i=1:length(L)
for j=1:length(rho);
for k=1:length(theta);
[x,y,z]=pol2cart(theta(k),rho(j),L(i));
X{i} = x ; Y{i} = y ; Z{i} = z ;
end
end
end
figure(1)
hold on
plot3(X,Y,Z)

채택된 답변

David Hill
David Hill 2020년 6월 9일
clear;
theta=0:10:360;
rho=0:.5:2;
L=-2:0.5:2;
[a,b,c]=meshgrid(theta,rho,L);
[x,y,z]=pol2cart(a,b,c);
plot3(x(:),y(:),z(:));
  댓글 수: 3
David Hill
David Hill 2020년 6월 10일
It just converts x matrix into a column vector (reshaping to 1 dimension)
Oday Shahadh
Oday Shahadh 2020년 6월 10일
Very good job David
Can I ask another thing pls?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2020년 6월 9일
A critical missing bit of information is missing from your questions. An error is thrown by the last line in your quesiton.
Error using plot3
Not enough input arguments.
This is because your plot3() inputs are cell arrays. It looks like your loops produce scalar values on each iteration so those values can be stored within a numeric array which will be much easier to use.
Your loops were overwriting the x,y,z values on each iteration of the j and k loops. The method below will store those values but it's up to you to make sure this makes sense in the context of whatever you're doing.
theta=0:10:360;
rho=0:.5:2;
L=-2:0.5:2;
% Numeric arrays, not cell arrays
% Also, preallocation to the correct size
X = nan(length(L), length(rho), length(theta)) ;
Y = X;
Z = X;
for i=1:length(L)
for j=1:length(rho)
for k=1:length(theta)
[x,y,z]=pol2cart(theta(k),rho(j),L(i));
X(i,j,k) = x ; Y(i,j,k) = y ; Z(i,j,k) = z ; % Store all of the value
end
end
end
As for plotting, it's not clear how you would like to use these values. plot3() creates a 2D plot but the inputs cannot have more than 2 dimensions and the x y z values above have 3 dimensions.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by