1

I have such a code;

figure;
    for a = 1:length(weekM')
        tday_cell = day_cell(week_cell == weekM(a));
        for b = 1:length(days)
            subplot(length(weekM'), 7, b);
            if strcmp(first_entranceM{a, b}, '0') 
                plot(peaks);
                l = [l; 0];
            else
                tms_cell = ms_cell(week_cell == weekM(a));
                ttms_cell = tms_cell(strcmp(tday_cell,days{b}));
                l = [l; ttms_cell];
                plot(ttms_cell);
            end
        end
    end

I want to plot graphs with subplot function as subplot(3,7,[1:2:3...]). That is, I want to plot graph which has 3 lines, and 7 graphs on each line.

But in my case, it only shows the last line on the first line, and I can't see the remaining graphs.

I'm sure that the data to plot previous graphs are not being lost, but I don't understand why there are missing graphs.

Could you help me to fix this problem?

yusuf
  • 3,591
  • 8
  • 45
  • 86
  • 1
    what exactly do you mean by the elipses in `subplot(3,7,[1:2:3...])`? Writing `subplot(3,7,[1:2:3])` would the plot a single axis over the first, second and third grid location, which sounds contrary to what you desire. Also, when you say "lines" are you referring to line plots or rows of the grid formed by calling subplot? – Delyle Jun 25 '15 at 15:30
  • I mean subplot(3,7,a) a = 1..2..3..4..5..6..7 – yusuf Jun 25 '15 at 15:31
  • 1
    length(weekM') = 3 presumably? What is the size and variable type of `ttms_cell`? – Delyle Jun 25 '15 at 15:37
  • 1
    You lack a basic understanding of how `subplot` works. I've marked this post as duplicate to a post I wrote a while ago. Read that, and you can figure out why your code isn't working. Hint: It's the `b` term in your `subplot` call. – rayryeng Jun 25 '15 at 15:56
  • 1
    @rayryeng: I don't think it's a duplicate of the linked question. The OP has not only the wrong addressing but needs a solution for his problem because it's not only about changing `b`. – Matt Jun 25 '15 at 16:07
  • 1
    @Matt - That's right. The `b` term needs to be changed. I didn't say that the `b` needs to be adjusted... it does need to be completely changed :) I would personally just put a counter in at the beginning, and increment by 1 each time a plot is made... but your `sub2ind` stuff works too. I'm still gonna leave it as a duplicate because the code still lacks basic understanding on how `subplot` works. – rayryeng Jun 25 '15 at 16:10
  • 1
    @rayryeng: To clarify, I meant the `b` in the function call of `subplot`. There the second argument should be 7. The OP probably tried different versions to get the right positions and ended up just posting his last attempt. – Matt Jun 25 '15 at 16:16
  • 1
    @Matt - I understand. OK, I'll reopen. – rayryeng Jun 25 '15 at 16:17
  • 1
    @rayryeng: Thanks... Check out my answer, it solves his problem. – Matt Jun 25 '15 at 16:22

1 Answers1

1

Your line with the subplot-statement needs to be changed. The first argument is correct. The second argument represents the number of columns and needs to be set to 7 here, since you have 7 days. The last argument is the index of the addressed subplot and needs to be changed in every iteration.

This index can be generated using the sub2ind-function, which calculates the index from the subscripts. The following line can be used instead of yours:

subplot(length(weekM'),7,sub2ind([7,length(weekM')],b,a));
Matt
  • 12,848
  • 2
  • 31
  • 53