repetitively running the code and selectively plotting
Show older comments
I am a new learner of Matlab and I am trying to learn to code with the loops. I am stucking with a problem.
I have table "a" which has multiple rows and 2 columns of table in matlab. I attached the file here.
1st column is time and 2nd column is data (q).
I want to make a loop for 6000 rows each and generate plot (data vs time). Before generating plots I want the user to see the starting time of each loop (the time from row 1,6001,12001) and let the user to decide the specific time to generate the specific plot related to that time.
I know my code is a lot of lacking and I am doing self learning. Can somebody help me out?
u = 1:1:6000 % I want to make it loop automatically from 1 to 100 but i don't know how
r = a.Time(u)
for i = 1:1:r
figure
plot
plot(a.t(i),a.q(i))
title(sprintf('Selling vs time from %s', ),r)
xlabel('time')
ylabel('selling $')
end
Answers (1)
Image Analyst
on 28 Oct 2022
First use readmatrix or readtable to read in the data and plot the whole thing with a single call to plot. Then use input to ask the user what part they want to plot. Then in your loop you need to plot one marker and then call hold on
plot(a.t(i), a.q(i), 'b.', 'MarkerSize', 8);
hold on;
5 Comments
uzzi
on 28 Oct 2022
Image Analyst
on 28 Oct 2022
A marker is they symbol you want to plot, like a dot or a square or a triangle or whatever. If you're going to use a loop to plot, then you can only plot a single symbol at a time, OR you can plot the whole array (as much as you have up until that point). But if you do the latter, don't use hold on since in that case, we DO want the next plot to blow away the prior one.
plot(a.t(1:i), a.q(1:i), 'b-.', 'LineWidth', 2, 'MarkerSize', 8);
uzzi
on 29 Oct 2022
Image Analyst
on 30 Oct 2022
How are you defnining a?
uzzi
on 1 Nov 2022
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!