Storing For Loop Data For Future Use In A 3D Plot

I wish to store three sets of data (X, Y, Z) acquired through a For loop in an array, but I am unsure of how to do so.
The end goal is to place all data onto a 3-D plot, which I assume I would be able to do once the data is in an array.
I am extremely new to matlab, so any assistance is greatly appreciated!
My current work is below:
data = xlsread('VelocityData.xlsx');
[rowsize, colsize] =size(data);
i=1;
j=10;
k=0;
for ii=2:rowsize
A=data(ii,1);
B=data(ii,2);
C=data(ii,3);
X=(((A)-((i)/3)));
Y=(((B)+6)/(j));
Z=(((C)-(2*(k))/(3)));
ii=ii+1;
i=i+1;
j=j+10;
k=(k-5);
end

 채택된 답변

Carter Allen
Carter Allen 2021년 12월 7일
data = xlsread('VelocityData.xlsx');
[rowsize, colsize] =size(data);
i=1; j=10; k=0;
for ii=2:rowsize
A=data(ii,1); B=data(ii,2); C=data(ii,3);
X(ii-1)=(((A)-((i)/3)));
Y(ii-1)=(((B)+6)/(j));
Z(ii-1)=(((C)-(2*(k))/(3)));
i=i+1;
j=j+10;
k=(k-5);
end
This seems to accomplish what you wanted. You need to index into a new position of X,Y, and Z for every iteration of the loop, otherwise you will override the previous values and they will remain scalar. Also, you don't need to update the index ii by yourself, the for loop will do that for you.
Alternatively, you could try something like
while ii <= rowsize
% all of your code goes here
ii = ii + 1
end

추가 답변 (1개)

dpb
dpb 2021년 12월 7일
What you're written seems like awfully peculiar manipulations to perform on the variables, but
V=[1:rowsize].'; % temporary vector to build addends
X=data(:,1)-V/3; % subtract i/3 from each row i
Y=(data(:,2)+6)./(10*[1:rowsize].'); % and by tens for column 2
X=(data(:,3)+2*5(V-1)/3; % and by -5 for column 3 (NB: - a - --> a +)
using MATLAB vector indexing addressing, the underlying reason there is a MATLAB! :)
plot3D should then work directly.
Experiment with the expression using V to see how they reproduce your explicit calculations. Such "tricks" are the key to using MATLAB effectively -- one trades memory for operations and the vectorized code operates very quickly once it is into the core MATLAB compiled code called/executed by the intrinsic math operators, etc.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2021년 12월 7일

답변:

dpb
2021년 12월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by