Minor grid-lines spacing issue

I have created a GUI using GUIDE and have two checkboxes, where one toggles minor and the other toggles major gridlines. They both work by activating the appropriate lines, but the minor grid is exactly under the major. (So in effect only the major are visible.)
How can I fix this issue so that minor grid-lines appear in between the major as would be expected?
Note: I am using MATLAB 2015a
Major Grid Active
Minor Grid Active
Required Grid

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 28 de Mzo. de 2017
Ben - how are you enabling and disabling the grid lines? If you are checkboxes are named checkbox1 and checkbox2 for the major and minor grid lines respectively, then the callbacks for both would be
function checkbox1_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
grid(handles.axes1,'on');
elseif get(handles.checkbox2,'Value') == 0
grid(handles.axes1,'off');
end
function checkbox2_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
grid(handles.axes1,'minor');
else
grid(handles.axes1,'minor');
if get(handles.checkbox1,'Value') == 0
grid(handles.axes1,'off');
end
end
We just need to add special cases to ensure that the major grid lines are turned off or left on depending upon whether the minor grid lines are off. See the attached code for an example.

5 comentarios

Ben Hall
Ben Hall el 28 de Mzo. de 2017
Editada: Ben Hall el 28 de Mzo. de 2017
Geoff, thanks for your reply.
I have something similar in the code currently being used. Note: in the opening function code. I specify that 'handles.minor' is equal to zero. As by default the check box will be unchecked (0.0).
GUI
% function majorGridCheck_Callback(hObject, eventdata, handles)
%
% if get(hObject, 'Value') == 1
% grid on
% elseif handles.minor ==0
% grid off
% elseif handles.minor ==1
% grid off
% grid minor
% end
%
%
% function minorGridCheck_Callback(hObject, eventdata, handles)
%
% if get(hObject, 'Value') ==1
% handles.minor = 1;
% else handles.minor = 0;
% end
% if get(hObject, 'Value') == 1
% grid minor
% else grid minor
% end
% guidata(hObject, handles);
end
My problem seems to be that the minor grid lines are "underneath" the major ones. So minor grid lines cannot be seen, as they do not show smaller divisions than major grid lines.
I'm not sure that the original print screens are clear. The window with the orange bar shows 'major' lines and the window with the blue bar shows 'minor' lines - and they occupy the same space.
Geoff Hayes
Geoff Hayes el 29 de Mzo. de 2017
Ben - I can't see the mini grid lines at all in your figure (with the blue bar). Your code is sound though and so should work so I can't help but wonder if there is something else at play. Can you share more of your code or at least enough of it that so that we can reproduce this problem?
Ben Hall
Ben Hall el 29 de Mzo. de 2017
That might be easier.
This part shows the plot of a line and the major and minor grid lines, which is the part I seem to be having an issue with. The other functions in the larger part of the script are unrelated to the grid lines functionality.
When I originally wrote this part of the code, the major and minor grid lines behaved as expected.
Geoff Hayes
Geoff Hayes el 30 de Mzo. de 2017
Ben - when I run your code, I can see the major and minor grid lines (they behave as expected). Am I misunderstanding something?
Ben Hall
Ben Hall el 29 de Abr. de 2017
I realise there has been a large delay in my reply to your message.
I'm still not sure what the problem with my GUI was, as I have 'retyped' my code into another script (it was identical to above) and now the gridlines behave as expected. I expect this is what you observed when running my code.
I have accepted your answer as the code was spot on to solve the problem, thank you.

Iniciar sesión para comentar.

Más respuestas (1)

Omanshu Thapliyal
Omanshu Thapliyal el 28 de Mzo. de 2017
Another workaround apart from the answer above could be to specify the 'XMinorTickValues'. This lets you control the resolution of the minor grid. You could do so by getting the axis handle as
ax = gca;
ax.XMinorGrid = 'on';
ax.XAxis.MinorTickValues = 260:10:380;
You could set the minor grid resolution in the following part of your callback and change the minor grid lines to wherever you want them to be.
if get(hObject, 'Value') == 1

1 comentario

Ben Hall
Ben Hall el 29 de Mzo. de 2017
Thanks Omanshu.
I attempted this workaround as an alternative to the current implementation, but received an error message. Upon some research, it appears that your suggestion was only introduced into MATLAB build 2015b - which is later than I have access to.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Mzo. de 2017

Comentada:

el 29 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by