How do i save each loop of data within a loop so i can plot it.
2 次查看(过去 30 天)
显示 更早的评论
This Code takes data from an excel sheet which contains two columns. The first column is the magnitude and the second is the direction of the vector. The code displays each x and y point but the variable 'x' and 'y' only save as the last calculated. What i need is to essentially take the excel data and graph the vectors.
heres the code:
clc
C=xlsread('homework16');
N=size(C,1);
i=1;
x=0;
y=0;
while i<=N
arrb(i,1)=C(i,1).*cosd(C(i,2));
arrb(i,2)=C(i,1).*sind(C(i,2));
i=1+i;
end
i=1;
while i<=N;
x=x+arrb(i,1);
y=y+arrb(i,2);
i=1+i;
end
0 个评论
回答(2 个)
Nick Hobbs
2015-10-29
I understand you would like to make sure the variable is saved during the loop. The issue looks like it may be due to variable scope. The following link will describe how to check variable scope in the editor and may help to resolve the issue.
0 个评论
Sudhanshu Bhatt
2015-10-29
Hi Christopher Lee,
As Nick has commented above the link will help you to better understand the scope of variables inside your code.
For example, you can save every computed value for X and Y by making them vectors and appending data to the vector after every iteration.
Demo code:
% Demo vectors to add to X and Y
a = [1,2,3,4,5,6,7,8];
b = [1,2,3,4,5,6,7,8];
% Declare X and Y as vectors
x = [0];
y = [0];
%Run for loop and save value inside X and Y in every iteration
if(length(a) == length(b))
for i = 1:length(a)
x = [x (x(i) + a(i))];
y = [y (y(i) + a(i))];
end
end
Thanks
Sudhanshu Bhatt
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!