how to plot a graph from data from a for loop in matlab

To plot a graph from data generated in a for loop in MATLAB, you can store the data in an array or matrix within the loop, and then use the plot function to generate the graph.

Here is an example:

main.m
% Initialize an empty array to store the data
data = [];

% Perform the calculations and store the data in each iteration of the loop
for i = 1:10
    % Generate some random data
    x = i;
    y = rand();
    
    % Store the data
    data = [data; x, y];
end

% Plot the data
plot(data(:,1), data(:,2))
305 chars
16 lines

In this example, we generate random data in each iteration of the loop and store it in data. Then, we use plot to create a graph using the first column of data as the x-values and the second column as the y-values.

Make sure to replace the calculations inside the loop with your own code, depending on the specific data you want to plot.

Note: By default, plot connects the data points with lines. If you want to plot only the points, you can use the scatter function instead of plot.

main.m
scatter(data(:,1), data(:,2))
30 chars
2 lines

This will show the data points as individual markers on the graph.

related categories

gistlibby LogSnag