Change x-axis grid lines

29 ビュー (過去 30 日間)
monkey_matlab
monkey_matlab 2015 年 10 月 21 日
コメント済み: Chad Greene 2015 年 10 月 21 日
Hello,
In my MWE below, I wanted to know how to add minor grid lines of 0.001 to the x-axis?
Here is my code:
% Select file
clear;
clc;
[FileName,PathName] = uigetfile('*.txt','Select data file');
fid = fopen( strcat(PathName,FileName) ,'rt' );
% Read the file and store into matrix v
i = 0;
v = [0 0];
while feof(fid) == 0
buffer = fscanf(fid, '%f', 2);
buffer = buffer';
if i == 0;
v = buffer;
else
v = vertcat(v,buffer);
end
i = i + 1;
end
% Frequency vector
x = v(:,1);
y = v(:,2);
plot(x,y);
grid on;
title('Spectrum @ 380.025MHz');
xlabel('Frequency (MHz)'); ylabel('Amplitude');
return
  1 件のコメント
Chad Greene
Chad Greene 2015 年 10 月 21 日
A note: Get into the habit of using k as a counter because in some cases Matlab will think you mean the sqrt(-1) when you use i or j. You can usually overwrite i and j safely, but when it causes problems it can be tough to debug.

サインインしてコメントする。

採用された回答

Chad Greene
Chad Greene 2015 年 10 月 21 日
You can turn on minor ticks by
set(gca,'XMinorTick','on')
and/or
set(gca,'XMinorGrid','on')
but exact values of the minor grid are hard to control. You may simply plot thin vertical lines at the values you desire by
plot(repmat(380.01:0.001:380.035,2,1),repmat([-140 0]',1,26),':','linewidth',0.5,'color',[.5 .5 .5])
hold on
plot(x,y,'b','linewidth',2)
I made the grid lines above quite thin and subtle because grids are only useful to viewers who are inspecting a plot with a straight edge. The muted grid is good enough for the detail-oriented viewer to see. For anyone who is glancing at the plot to get the big picture, grids are nothing but clutter, IMO.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by