How do i save each loop of data within a loop so i can plot it.

1 回表示 (過去 30 日間)
Christopher Lee
Christopher Lee 2015 年 10 月 27 日
回答済み: Sudhanshu Bhatt 2015 年 10 月 29 日
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

回答 (2 件)

Nick Hobbs
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.

Sudhanshu Bhatt
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by