代碼
%%// FILENAME
filename = 'sid.xls';
%%// OPTION 1: If there is a lot of data, XTickLaabels and XTicks would clutter up.
%%// To Avoid that, define the number of XTickLabels needed,
%%// otherwise set it as NaN to use all x-data.
XTickLabel_count = 3;
%%// OPTION 2: If you would like to show the time only with dates
time_only = false;
%%// OPTION 3: If you would like to show the XTickLabels as 90 degrees rotated
XTickRot = false;
%%// Read in data
[num,text1,raw] = xlsread(filename);
%%// Account for 12AM times, which are not read in text1. Append that data.
split1 = regexp(text1,'\s','Split');
zero_times = cellfun(@numel, split1)==1;
text1(cellfun(@numel, split1)==1) = mat2cell([char(text1(zero_times)) repmat(' 00:00:00',nnz(zero_times),1)],ones(1,nnz(zero_times)));
%%// Get the time only data without dates
split1 = regexp(text1,'\s','Split');
split_text = vertcat(split1{:});
time_text = split_text(:,2);
%%// Use the time only data for XTickLabels
if time_only
text1 = time_text;
end
%%// Select few XTickLabels from the entire X-data or whole respectively
%%// and store as text2
if ~isnan(XTickLabel_count)
XTickIntv = round(numel(text1)/XTickLabel_count);
text2 = cell(size(text1));
text2(1:XTickIntv:end)=text1(1:XTickIntv:end);
else
text2 = text1;
end
%%// Plot
figure,plot(num)
set(gca, 'XTickLabel',text2, 'XTick',1:numel(text2))
if XTickRot
xticklabel_rotate([],90,text2);
end
set(gca, 'Ticklength', [0 0]) %%// Remove XTicks but keep XTicklabels
return;
注:此代碼使用XTICKLABEL_ROTATE from Mathworks File-exchange
方法1:使用XTickLabel_count = 3
time_only = false
和XTickRot = false
方法2:使用XTickLabel_count = 5
time_only = true
和XTickRot = false
方法3:使用XTickLabel_count = 10
time_only = true
和XTickRot = true
嗨, 當我繪製它在我的MATLAB readi ng excel文件,我從第一種方法得到: http://tinypic.com/view.php?pic=j8noqt&s=8#.U0SoA71lfDc 看到情節創建了一些黑線而不是日期時間滴答聲。 第二種方法繪製此圖: http://tinypic.com/view.php?pic=15xqree&s=8 請參閱沒有時間標記只是一些整數值。 你的第三種方法繪製了類似於第一種方法的東西+它也顯示了一行錯誤消息:4 這裏有什麼問題? – user3511734
嗨, 這是我的原始Excel文件。你能告訴我用你的代碼繪圖的問題是什麼? https://drive.google.com/file/d/0ByntE7ZqdSzoNkRhb0p3ckl0Mzg/edit?usp=sharing – user3511734
@ user3511734檢出已編輯的代碼,並確保您拿起腳本以從[這裏]旋轉「XTickLabels」( http://www.mathworks.in/matlabcentral/fileexchange/3486-xticklabelrotate) – Divakar