Issue with TickLabelInterpreter using uifigure

4 views (last 30 days)
I need to display the tick labels on multiple lines and I was able to come up to a good solution using latex code. Unfortunately, this solution doesn't work when using uifigure(). Any tips?
tmpLabels = {'Session 1','Session 2','Session 3','Session 4'};
tmpLabels2 = {'(Condition 1)','(Condition 2)','(Condition 1)','(Condition 2)'};
n = numel(tmpLabels);
tmpLabels3 = cell(1,n);
for tmpId = 1:n
tmpLabels3{tmpId} = sprintf('\\begin{tabular}{c} %s \\\\ %s \\end{tabular}',tmpLabels{tmpId},tmpLabels2{tmpId});
end
% This works fine
tmpF = figure('Position',[650,550,700,400]);
tmpT = tiledlayout(tmpF,1,3,'TileSpacing','tight','Padding','compact');
title(tmpT,'Figure');
tmpAx = nexttile(tmpT,[1 2]);
set(tmpAx,'XLim',[0.8 4.2],'XTick',1:4,'XTickLabel',tmpLabels3,'TickLabelInterpreter','latex','Box','on','YGrid','on')
% The same code doesn't work when using uifigure() in place of figure()
tmpUiF = uifigure('Position',[650,550,700,400]);
tmpUiT = tiledlayout(tmpUiF,1,3,'TileSpacing','tight','Padding','compact');
title(tmpUiT,'UI Figure');
tmpUiAx = nexttile(tmpUiT,[1 2]);
set(tmpUiAx,'XLim',[0.8 4.2],'XTick',1:4,'XTickLabel',tmpLabels3,'TickLabelInterpreter','latex','Box','on','YGrid','on')
  2 Comments
Ryan Romeo
Ryan Romeo on 17 Feb 2023
Edited: Walter Roberson on 21 Apr 2023
I'm assuming here that you definitely want to use the 'latex' interpreter, and you don't want to use a workaround that involves manually positioning text boxes.
I found a passable way to do it without using the \tabular LaTeX environment. Curiously, the line break (\\) only registers when I put the entire label into a math environment (between $ symbols). Each row of text is enclosed in a \text environment to avoid the math environment font. Also since I dropped the table environment, the code loses its centering properties, so I manually shifted the top row with ~'s. See code below:
% New label cell array
tmpLabels4 = cell(1,n);
for tmpId = 1:n
tmpLabels4{tmpId} = [ '$~~~~\text{' tmpLabels{tmpId} '}\\ \text{' tmpLabels2{tmpId} '}$' ];
end
If you insert the above code before your 'uifigure' code, and then change the 'XTickLabel' argument from tmpLabels3 to tmpLabels4, it should give you something close to what the regular figure's labels looks like.
Tommaso Mastropasqua
Tommaso Mastropasqua on 19 Feb 2023
Edited: Walter Roberson on 19 Feb 2023
Thank you for your prompt response.
I hope the support team can fix the LaTeX environment within uifigure(), because the \tabular function is the only solution I have found for an efficient horizontal alignment of multiple labels.
May I follow up with another error I get when trying to save a uifigure containing a uitable? Or maybe should I opena a new request?
tmpLabels = {'Session 1','Session 2','Session 3','Session 4'};
tmpLabels2 = {'(Condition 1)','(Condition 2)','(Condition 1)','(Condition 2)'};
n = numel(tmpLabels);
tmpLabels3 = cell(1,n);
for tmpId = 1:n
tmpLabels3{tmpId} = sprintf('$~~~$ %s $\\\\$ %s}',tmpLabels{tmpId},tmpLabels2{tmpId});
end
tmpUiF = uifigure('Position',[550,550,850,400]);
tmpUiT = tiledlayout(tmpUiF,1,3,'TileSpacing','tight','Padding','compact');
title(tmpUiT,'UI Figure');
tmpUiAx = nexttile(tmpUiT,[1 2]);
set(tmpUiAx,'XLim',[0.8 4.2],'XTick',1:4,'XTickLabel',tmpLabels3,'TickLabelInterpreter','latex',...
'Box','on','YGrid','on')
tmpUiAx = nexttile(tmpUiT);
axis(tmpUiAx,'off')
drawnow
tmpUit = uitable(tmpUiF,'Data',magic(3),'ColumnName',{},'RowName',{},...
'Units','Normalized','Position',tmpUiAx.Position,'FontSize',14);
tmpS = uistyle('HorizontalAlignment','center','Interpreter','latex');
addStyle(tmpUit,tmpS)
tmpS = uistyle('BackgroundColor',[0.94 0.94 0.94]);
addStyle(tmpUit,tmpS,'row',1:2:3)
tmpS = uistyle('BackgroundColor','w');
addStyle(tmpUit,tmpS,'row',2:2:3)
tmpS = uistyle('BackgroundColor',[0.8 0.8 0.8],'FontWeight','bold');
addStyle(tmpUit,tmpS,'row',1)
tmpS = uistyle('Icon','success','IconAlignment','rightmargin');
addStyle(tmpUit,tmpS,'cell',[2 3; 3 3]) %--- This line triggers the error using exportapp ---%
exportapp(tmpUiF,'myUifig.png');

Sign in to comment.

Answers (1)

Divyanshu
Divyanshu on 21 Apr 2023
This solution is for the error you are getting while exporting the figure into a file.
One possible workaround to resolve the issue is by using ‘exportgraphics’ function instead of the exportapp’.
The reason is that the ‘exportappfunction is not able to export the figure into the file because of the ‘success-icon’ added to the cells of the table tmpUit, just before exporting the figure.
Please refer the following documentation for further information about exportgraphicsfunction:
(Note: The above workaround is for the second problem, one you mentioned in the comments.)
  1 Comment
Tommaso Mastropasqua
Tommaso Mastropasqua on 21 Apr 2023
Thank you for your suggestion. However, upon using the 'exportgraphics' function instead of 'exportapp', I received a warning indicating that the UI components won't be included in the output. The 'exportapp' function is recommended for including UI components. Additionally, the output generated by 'exportgraphics' does not contain the 'uitable', which is crucial for my requirements.

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!