0
votes

I have a sequence with data and an offset. I'm asked to plot a stem() graph of the data, starting at the offset. I have figured out the data part (the easy part) and how to change the window to include the offset, but when I plot the graph, it shows the offset with a value of zero and zeros until 1 where the sequence.data will start and plot points.

methods
    function s = sequence(data, offset)
        s.data = data;
        s.offset = offset;
    end
function stem(x)
        % STEM Display a Matlab sequence, x, using a stem plot.
        stem(x.offset,x.data);
        axis([x.offset x.offset+length(x.data) 'auto' 'auto']);

    end

I need to figure out how to "move" my x.data to my x.offset and start stem plotting there.

1
I don't fully understand your question, but maybe use NaNs instead of zeros? NaNs don't get plotted - Luis Mendo
All of the stem() examples that I've seen start the plot at 0, but I need my plot to start at the offset of the sequence. For example, if my sequence is x = sequence([1 2 3 4 5], -3) then my stem plot would be [-3,1][-2,2][-1,3][0,4][1,5]. So far, I can't get my plot to start at -3. It'll currently be [-3,0][-2,0][-1,0][0,0][1,1][2,2][3,3][4,4][5,5]. - MikaelCMiller

1 Answers

1
votes

I don't understand why you simply can't add an x offset while keeping the y data the same?

For instance, given your example in your comments above:

x = 0:4;
y = 1:5;

This is what the original graph looks like, as well as shifting the graph to the left by 3 (-3):

stem(x,y,'b');
hold on;
stem(x-3,y,'r');

This is what I get:

enter image description here

The blue data is the original, while the red is the shifted instance to the left by 3. As you can see, the y data is the same, but the x points move to the left by 3. What your code is actually doing is that it does not shift the actual data. You are only changing the display range of your stem plot. As such, you should really be doing this:

methods
    function s = sequence(data, offset)
        s.data = data;
        s.offset = offset;
    end
    function stem(x)
        % STEM Display a Matlab sequence, x, using a stem plot.
        %// First define sequence from [0,N-1]
        vals = 0:numel(x.data)-1;
        %// Now use the above and manually shift the x coordinate
        stem(vals+x.offset,x.data);
    end

I'm going to assume that your data on the x-axis starts counting at 0, and so we will declare a sequence from 0 up to N-1 where N is the total number of elements that you have. Once we declare this sequence, when it's time to draw the stem plot, we simply add an offset to this sequence and use this as the x data. The y data should stay the same.

However, I would argue that creating a custom class for implementing this addition to stem is less readable than what I originally did above. If this is a requirement for whatever you're developing, then certainly go ahead and do it this way, but I don't really think it's necessary.