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

1 回表示 (過去 30 日間)
Nathan McDermott
Nathan McDermott 2021 年 12 月 7 日
回答済み: dpb 2021 年 12 月 7 日
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.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by