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

figurematlab guiplots

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?

Best Answer

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.