1
votes

When creating a plot in Matlab the following error is produced:

matlab plot problem

I have tried many different methods of trying to find the problem and solving it. I think I have identified that the cause is because I am using multiple xlim commands. Here is my code:

    figure
    plot(datee,[allfile.bytes],'k','LineWidth',1.5);
    hold on;
    plot(datee,EMA,'--b','LineWidth',0.75);
    title(sprintf('File size of %s',[token{1},dateno{1},name]));
    xlabel('Month/Day');
    set(gca, 'YTickLabel', num2str(get(gca,'YTick')'/1024,'%0.0f'));
    datetick('x','mm/dd');
    ylabel('Bytes in Kb');
    plot(xlim,[ulimit ulimit], 'r')
    plot(xlim,[ASize{jj} ASize{jj}], 'g')
    plot(xlim,[llimit llimit], 'r')
    axis tight;
    axis 'auto y';
    hlegend=legend('File Size','Average/EMA','Upper/Lower limit','Current Size');
    set(hlegend,'Location','Best')

I do not understand why the problem occurs as it does.. And also how to solve it! Has anyone experienced this before? Since I think the xlim command is the fault, are there any other alternatives to creating a solid straight line across the whole plot?

1
Can you post a runnable example? We don't have your variables datee etc - Luis Mendo
for the straight line issue: line('XData',[xlim(1) xlim(2)],'YData',[y(1) y(2)]'Color','r'); where xlim is obtained with get(gca,'XLim'); and y is up to you - Benoit_11
what value does jj have? As in this line: plot(xlim,[ASize{jj} ASize{jj}], 'g') - Benoit_11
@Benoit_11 jj is a number between 1 and 20, it rotates through making plots for various scenarios. - AMcNall
@AMcNall ok I see. If you replace the calls to plot(xlim,...) with the line I wrote in my above comment does it solve the problem? - Benoit_11

1 Answers

0
votes

Okay, I am unsure as to why this works, but I changed the code order very slightly to this:

        figure
    plot(datee,[allfile.bytes],'k','LineWidth',1.5);
    hold on;
    plot(datee,EMA,'--b','LineWidth',0.75);
    title(sprintf('File size of %s',[token{1},dateno{1},name]));
    xlimit = get(gca,'XLim');
    plot(xlim,[ulimit ulimit], 'r')
    plot(xlim,[ASize{jj} ASize{jj}], 'g')
    plot(xlim,[llimit llimit], 'r');
    xlabel('Month/Day');
    set(gca, 'YTickLabel', num2str(get(gca,'YTick')'/1024,'%0.0f'));
    datetick('x','mm/dd')
    ylabel('Bytes in Kb');
    axis tight;
    axis 'auto y';

And it no longer reproduces the above bug... I have no idea why though. If anyone ever has any suggestions/ideas, please let me know, I am very curious. Hope this helps anyone with the same problem in the future.