How do i save each loop of data within a loop so i can plot it.
64 views (last 30 days)
Show older comments
Christopher Lee on 27 Oct 2015
Answered: Sudhanshu Bhatt on 29 Oct 2015
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:
Nick Hobbs on 29 Oct 2015
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 on 29 Oct 2015
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 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 = ;
y = ;
%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))];
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!