Taking data from loops and the plotting them outside the loop
Show older comments
I need to plot a graph based upon the answers given (there's a prompt window before this).
In my for loop, I have an if, elseif, and else statement, which gives 3 very different values. Should I try to put all these into 1 cell array? How can I access the data of each array and then plot it?
Or, if I do this step (make a matrix and not an array) and construct a matrix for each of the 3 options. So if it were like:
h = 0:1:10
y = zeros(size(h));
v = zeros(size(h));
o = zeros(size(h));
prompts = {'enter 1'};
dlg = 'title';
hold on
for i=1:5
A = inputdlg(prompts, dlg);
Aa = str2double(A);
x = Aa(1);
if x == 1
y(i,:) = 1000*h;
m = m + 1;
plot(h,y(i,:));
elseif x == 0
v(i,:) = h+1;
u = u + 1;
plot(h,v(i,:));
else
o(i,:) = -h*5;
c = c + 1;
plot(h,o(i,:));
end
end
end
This will give 3 different matrices of the 3 different types of answers. When I used this option, this method will generate a new row in the matrix of each output. So if I'm on loop 5, and the if statement is triggered, and I triggered the same statement in the very first loop, then I'll get a matrix like:
v =
1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11
How can I take the data from the rows that have nonzeros and then plot them with different boundaries on the x-axis?
Accepted Answer
More Answers (0)
Categories
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!