creating graph from text file where axis X is date

2019-08-13 23:50发布

问题:

I still can not create something usable in matlab.

The task is pretty simple. I have two files: devideHistory.log deals.log In both files lets consider only first two column. So first file contains X and Y which should form graphic. Second file contains X and Y of markers that should be displayed on the graphic. With help of this community such program was created

clear

fDevide = fopen('devideHistory.log');
data = textscan(fDevide, '%f:%f:%f:%f %f,%f %f,%f');
fclose(fDevide);

% hh:min:sec:millisec
secvec = [60*60 60 1 1e-3];
x = [data{1:4}] * secvec';

flvec = [1 1e-16];
y = [data{5:6}] * flvec';

xindays = x / (24*60*60);

plot(xindays, y);
% set(gca, 'YTickLabel', get(gca,'YTick'))
% datetick('x', 'HH:MM:SS');

hold on

fDeals = fopen('deals.log');
data = textscan(fDeals, '%f:%f:%f:%f %f,%f %f,%f %f,%f %f');
fclose(fDeals);

% hh:min:sec:millisec
secvec = [60*60 60 1 1e-3];
x = [data{1:4}] * secvec';

flvec = [1 1e-16];
y = [data{5:6}] * flvec';

xindays = x / (24*60*60);

plot(xindays, y, 'go','MarkerSize',6,'LineWidth',3);

% i need to set enough precision on Y but this doesn't work because
% while zooming labels doesn't update
set(gca, 'YTickLabel', get(gca,'YTick')) 

% i want to have "time" on X but this doesn't work because
% while zooming new labels doesn't appear
datetick('x', 'HH:MM:SS');

Result:

But I have two problems with zooming feature:

  • while zooming in labels from X disappear and as a result there are not labels at X at all how to always display "labels" on axis X
  • while zooming labels on Y is not updated as a result markers are displayed on the wrong position why while zooming marker changes position?

Well my task is pretty simple and straightforward. Are there simple and straightforward solution in matlab for it? Code above already contains several "hacks" and still doesn't work as expected. I'm afraid contining hacking it doesn't make sense. Could someone suggest another approach? Probably matlab just not suitable for my needs?

回答1:

  1. Download http://www.mathworks.com/matlabcentral/fileexchange/36254-ticklabelformat
  2. Remove your own tick labeling calls, these ones:

    set(gca, 'YTickLabel', get(gca,'YTick')) 
    datetick('x', 'HH:MM:SS');
    
  3. Replace them with

    ticklabelformat(gca,'y','%g')
    ticklabelformat(gca,'x',{@tick2datestr,'x','HH:MM:SS'})
    
  4. Make a new helper function tick2datestr.m containing:

    function tick2datestr(hProp,eventData,axName,dateformat)    %#ok<INUSL>
        hAxes = eventData.AffectedObject;
        tickValues = get(hAxes,[axName 'Tick']);
        tickLabels = arrayfun(@(x)datestr(x,dateformat),tickValues,'UniformOutput',false);
        set(hAxes,[axName 'TickLabel'],tickLabels);
    end
    

most of this helper function is btw copied straight out of ticklabelformat.



Yes, you can consider this as hacking, but as long as TheMathworks doesn't implement your desired 'straightforward' solution, you have to create your own functionality.