1

I have been trying to use the subplot function in Matlab, using the following code:

x = 0:10;
figure
subplot(2,2,1); plot(x,data_matrix(1,:))
subplot(2,2,2); plot(x,data_matrix(2,:))
subplot(2,2,3); plot(x,data_matrix(3,:))
subplot(2,2,4); plot(x,data_matrix(4,:))

However, when I run this simple code, the only thing that displays is a single figure with a plot of the last data vector, data_matrix(4,:). No errors show up. Rather, subplots are simply not being generated. Am I doing something incorrectly, or is there a potential error with my programming environment that's keeping me from displaying subplots?

9
  • clear everything, and comment out 'figure'. Does that help? Commented Jun 30, 2017 at 17:53
  • No, I still get the same result. Commented Jun 30, 2017 at 17:57
  • Transpose matrices likes data_matrix(1,:).'. Commented Jun 30, 2017 at 17:57
  • I just tried that, and it also doesn't work. Commented Jun 30, 2017 at 18:05
  • Restart matlab? Commented Jun 30, 2017 at 18:06

1 Answer 1

1

Are you sure data_matrix(1,:) contains what you expect? You might want to check that out. Also, it'd great if you provided the minimal piece of code that reproduce your problem. Provided piece doesn't reproduce the problem because you don't provide access to data_matrix matrix which I suspect is causing the problem, because running this piece of code

x=0:10;
data_matrix=rand(4,11);

figure
subplot(2,2,1); plot(x,data_matrix(1,:))
subplot(2,2,2); plot(x,data_matrix(2,:))
subplot(2,2,3); plot(x,data_matrix(3,:))
subplot(2,2,4); plot(x,data_matrix(4,:))

works perfectly, as you can see below (I used a rand to recreate a well-formed data_matrix matrix)

output of piece of code

Sign up to request clarification or add additional context in comments.

6 Comments

data_matrix is simply a 4 x 11 matrix containing decimals. I have already checked the data to make sure there is nothing wrong with it.
When I run the code you provided, using a randomly created data_matrix, I still have the same issue. Only one plot is displayed.
Which version of matlab are you using? Run ver on matlab's prompt
I am running the following version of Matlab: 8.5.0.197613 (R2015a).
Hmm, that is a modern version so there should not be any problems. Instead of subplot(m,n,p) try subplot mnp and run again your code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.