Need help plotting multiple graphs

조회 수: 3 (최근 30일)
Donny Carsillo
Donny Carsillo 2019년 10월 24일
댓글: Donny Carsillo 2019년 10월 25일
I am currently trying to create a lever game where you pick a lever to "pull" and it gives you a number. I would like to grpah thesepoints but I am having trouble updating the new y value as I go through it again it will set all the y's the same. I also would like to figure out if I can have a different graph for each lever so I can look at the numbers I get from each lever that is "pulled."
code:
levergamequestion.PNG
I ran my program while only pulling lever 1 to show what has happened. As you can see on the first number it works great but after that the y value gets updated and does not store the old y for 1 or 2 x value.
levergamequestion1.PNG
levergamequestion2.PNG
Any help will be greatly appreciated.

답변 (1개)

Guillaume
Guillaume 2019년 10월 24일
Please, don't post screenshots of code. We can't copy/paste screenshots into matlab to test your code. Post the code as text (and format it using the code button) or attach as a m file.
Numbered variables are always a bad idea. Use indexing instead (i.e lever(1), lever(2), etc. instead of lever1, lever2, etc. If you do that then you don't need your if statements and the code will be much simpler.
lever = zeros(1, 4)
ranges = [1 4;
1 2;
2 5;
2 3] %range of numbers for each lever
figure; hold('on');
for i = 1:10
lever = input('...'); %since I can't copy your code, I'm not bothering rewriting your message
randomnumber = randi(ranges(lever, :)); %no need for if we just index the row of ranges to get the range matching the level
plot(i, randomnumber, '*'); %no idea what you were trying to plot
end
I've no idea what sort of plotting you were trying to do (what you have doesn't make sense) but the above is functionally equivalent to your code. See how much shorter it is because I use indexing.
Note that there is no difference between
y = a4;
and
y = [a4]; %concatenate just ONE element. You get the one element back.
so it's unclear what you were trying to do with these brackets.
Your current plotting plots:
  • for lever 1: plot(1, randomnumber, '*-') so just plots one point
  • for lever 2: plot(1:2, randomnumber, '*-') so plots twice the same point
  • for lever 3: plot(1:3, randomnumber, '*-') so plots three times the same point
  • for lever 4: plot(1:4, randomnumber, '*-') so plots four times the same point
Each plot calls erase previous plots (unless you've issued a hold('on') before running your code.
  댓글 수: 1
Donny Carsillo
Donny Carsillo 2019년 10월 25일
I was able to figure out what I needed to do thanks for the help. Sorry for not copying the code and using screenshots. Was my first time asking a quesiton here.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by