How to completely change the title of a plot when switching between two cases?

17 views (last 30 days)
Good morning! I have a problem with a GUI in Matlab. I have a plot, in the main window of the GUI, which is updated when switching from one case to the other of a "switch" button. Everything is actually updated but the title: when I switch to the second case, it is overwritten. I tried to use the "drawnow" command, the "refresh", also "refreshdata", but none of them works (all have left traces in the code...). The faulty code is:
-----------------------------------------------------
switch DataOpt
case 1
fig = gcbf;
refresh(fig);
ax1 = gca;
set(ax1,'XLim',[Data.lambda(1),Data.lambda(end)]);
set(ax1,'XColor','k','YColor','k');
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
set(ax2,'YTickLabel','');
set(ax2,'XLim',[1,400]);
set(ax2,'YLim',[min(Data.data(FixedDelay,:)),max(Data.data(FixedDelay,:))]);
set(ax1,'YLim',[min(Data.data(FixedDelay,:)),max(Data.data(FixedDelay,:))]);
XLim1=get(ax1,'XLim');
xinc1=(XLim1(2)-XLim1(1))/5;
YLim1=get(ax1,'YLim');
yinc1=(YLim1(2)-YLim1(1))/5;
set(ax1,'XTick',[XLim1(1):xinc1:XLim1(2)],...
'YTick',[YLim1(1):yinc1:YLim1(2)]);
hl1 = line(Data.lambda,Data.data(FixedDelay,:),'Color','b','Parent',ax1);
xlabel(ax2,'# pixel');
xlabel(ax1,'\lambda (nm)');
ylabel(ax1,'Fluorescence');
title(['Spectrum, delay = ' num2str(Data.delays(FixedDelay)) ' ns, raw']'');
case 2
drawnow update;
fig = gcbf;
refresh(fig);
%refreshdata;
ax1 = gca;
set(ax1,'XLim',[Data.lambda(1),Data.lambda(end)]);
set(ax1,'XColor','k','YColor','k');
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
set(ax2,'YTickLabel','');
set(ax2,'XLim',[1,400]);
set(ax2,'YLim',[0,1]);
set(ax1,'YLim',[0,1]);
XLim1=get(ax1,'XLim');
xinc1=(XLim1(2)-XLim1(1))/5;
YLim1=get(ax1,'YLim');
yinc1=(YLim1(2)-YLim1(1))/5;
set(ax1,'XTick',[XLim1(1):xinc1:XLim1(2)],...
'YTick',[YLim1(1):yinc1:YLim1(2)]);
hl1 = line(Data.lambda,Data.dataNorm(FixedDelay,:),'Color','b','Parent',ax1);
xlabel(ax2,'# pixel');
xlabel(ax1,'\lambda (nm)');
ylabel(ax1,'Fluorescence');
title(['Spectrum, delay = ' num2str(Data.delays(FixedDelay)) ' ns, normalized']'');
end
------------------------------------------------------------------------------------
Has anybody idea of what do I err?

Accepted Answer

Geoff Hayes
Geoff Hayes on 23 Oct 2014
Fede - what do you mean by Everything is actually updated but the title: when I switch to the second case, it is overwritten. So the title is the same for both cases or the old title is visible beneath the new title?
I think that part of the problem is that the code is building/creating a new axes, ax2, for both cases. So in the first case block, the code sets
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),...);
and then the title is written to ax2 since the code does not specify which axes to write to.
When the second block is executed, the same thing (as above) is done, except now, the current axes (as returned by gca) may very well correspond to ax2. We then create a new axes as
ax2 = axes('Position',get(ax1,'Position'),);
which results in a third axes that the title is written to.
This can be simulated as
figure;
fig = gcf;
refresh(fig);
ax1 = gca;
set(ax1,'XColor','k','YColor','k');
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
set(ax2,'YTickLabel','');
set(ax2,'XLim',[1,400]);
xlabel(ax1,'test')
title('My Default Title - Test 1');
We can then get the axes handles for this figure as
hAxes = findobj('Parent', fig, 'Type', 'axes');
which is a 2x1 vector, where hAxes(1) corresponds to the last added axes, so ax2, and hAxes(2) corresponds to the first added axes, so axes1. We can see which has the title applied to it by executing
get(get(hAxes(2),'Title'),'String')
ans =
''
which returns an empty string, which must meant that the title has been "assigned" to ax2
get(get(hAxes(1),'Title'),'String')
ans =
My Default Title - Test 1
Now if we re-run the majority of the above code, with a new title
fig = gcf;
refresh(fig);
ax1 = gca;
set(ax1,'XColor','k','YColor','k');
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
set(ax2,'YTickLabel','');
set(ax2,'XLim',[1,400]);
xlabel(ax1,'test')
title('My Other Title - Test 2');
We see that one title is above the other, and that
hAxes = findobj('Parent', fig, 'Type', 'axes');
is now an array with three axes handles. Likewise, we'd see that the new title was assigned to the newly added axes, hAxes(1).
-----
I know that you are executing your code from within a callback (due to the gcbf call). Are you using GUIDE to create your GUI? If so, then I would recommend that you create the second axes in the OpeningFcn of your GUI and not repeat this logic for every time that the user switches between the first and second (and maybe more?) cases. Then save the axes handles to the handles structure which you can then access in this callback via
handles = guidata(fig);
Then you would just access the axes handles via
handles.ax1
handles.ax2
or whatever you have named them. When it comes time to writing the title, you would then use either of these handles
title(handles.ax1, 'My title...');
If you are not using GUIDE, then you will want to do something similar - create the axes outside of the callback, and use one of these handles to write the title to.
  3 Comments
Geoff Hayes
Geoff Hayes on 24 Oct 2014
Fede - a problem with the above is that you are still creating an axes every time that your above callback is being called. So you will end up with multiple axes. Why don't you move this code into the OpeningFcn of your GUI?
While your use of t and ti to manage the handles to the title allows you to display only one title at a time, the real fix to this problem is to define two axes only, and display the title on one of those axes.
What is the rationale behind using these two axes, and why not create both of them using GUIDE as opposed to writing code to create the second axes?
Fede
Fede on 27 Oct 2014
I tried to move the creation of the axes in the OpeningFcn, but there raised another problem: when I switched the button after having performed any other operation on the GUI, the problem of overwriting came out again (while it was actually fixed when switching only between these two cases).

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!