How can I add string character description in the X axes instead of numbers? Is there a way to enhancer this code?

2 views (last 30 days)
How can I add string character description in the X axes instead of numbers?
Please see code below:
clear all; %get rid of everything in memory
close all; %close all open figures
%X axis
Test_Cases = { 'A in A', 'B in B', 'C in C', 'D in D', 'E in E', 'F in F' };
%Y axis
MaxorMin='min' %'max' or 'min'
if strcmpi(MaxorMin, 'max')
C_p_single_Tap= [0.450,0.383,0.513,1.683,1.494;
0.470 0.559 0.504 1.752 1.699;
0.665 0.559 0.431 1.483 1.582;
0.800 0.770 0.454 1.757 1.699;
0.663 0.653 0.199 1.223 1.934;
9.904 20.387 13.538 17.383 10.242];
C_p_area_average= [0.110 0.078 0.086 0.857 0.569;
0.126 0.093 0.091 0.976 0.595;
0.116 0.102 0.081 0.858 0.581;
0.120 0.066 0.067 0.527 0.845;
0.116 0.102 0.081 0.858 0.581;
1.058 2.527 6.418 3.411 1.840];
GCp_CC_ASCE=[0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0];
elseif strcmpi(MaxorMin, 'min')
C_p_single_Tap= [-1.836 -2.556 -4.615 -1.827 -2.702;
-2.218 -4.210 -7.497 -2.459 -4.187;
-1.865 -3.626 -6.552 -2.759 -2.480;
-2.541 -4.080 -7.459 -2.948 -4.080;
-1.591 -4.506 -11.288 -2.225 -1.300;
-13.129 -23.544 -39.372 -16.131 -19.103];
C_p_area_average= [-0.918 -0.760 -0.883 -0.926 -0.901;
-0.958 -0.975 -1.396 -0.882 -1.036;
-0.870 -1.014 -0.791 -0.765 -0.893;
-1.142 -0.920 -1.375 -1.045 -1.090;
-0.673 -0.814 -1.472 -0.629 -0.602;
-5.510 -3.513 -6.362 -6.582 -5.826];
GCp_CC_ASCE=[-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4];
end %if min or max
clear data
screensize = get( groot, 'Screensize' );
figure('Position', screensize)
t=tiledlayout(1,5,'TileSpacing','none');
for i = 1:5
plot(nexttile,1,C_p_single_Tap(1,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(1,C_p_area_average(1,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(2,C_p_single_Tap(2,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(2,C_p_area_average(2,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(3,C_p_single_Tap(3,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(3,C_p_area_average(3,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(4,C_p_single_Tap(4,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(4,C_p_area_average(4,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(5, C_p_single_Tap(5,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(5,C_p_area_average(5,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(6,C_p_single_Tap(6,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(6,C_p_area_average(6,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
yline(GCp_CC_ASCE(:,i),'--','LineWidth',3, 'Color', [1 0 0]);
A=min(min(C_p_single_Tap),min(C_p_area_average));
B=max(max(C_p_single_Tap),max(C_p_area_average));
if strcmpi(MaxorMin, 'max')
text(3,max(B)+1.55, "ZONE " + i ,'FontSize', 26,...
'HorizontalAlignment', 'center', 'VerticalAlignment',...
'middle','Interpreter', 'latex');
elseif strcmpi(MaxorMin, 'min')
text(3,max(B)+1.55, "ZONE " + i ,'FontSize', 26,...
'HorizontalAlignment', 'center', 'VerticalAlignment',...
'middle','Interpreter', 'latex');
end
ax.XTickLabel= sprintf('Test: %i', Test_Cases{mod(i-1,length(Test_Cases))+1});
end
linkaxes(t.Children)
xlim([-1 7])
xlabel(nexttile(3),'{\rm{Test case number}}','Interpreter','Latex','FontSize', 26);
ylabel(nexttile(1),'${C_p}$','Interpreter', 'Latex','FontSize', 26);
set(t.Children(1:4), 'YTickLabel', [])
rect = [0.90, -0.1, 1, 1];
set(h,'FontSize',26,'Position', rect);

Accepted Answer

Voss
Voss on 23 Jan 2022
Edited: Voss on 23 Jan 2022
Something like this?
clear all; %get rid of everything in memory
close all; %close all open figures
%X axis
Test_Cases = { 'A in A', 'B in B', 'C in C', 'D in D', 'E in E', 'F in F' };
%Y axis
MaxorMin='min' %'max' or 'min'
MaxorMin = 'min'
if strcmpi(MaxorMin, 'max')
C_p_single_Tap= [0.450,0.383,0.513,1.683,1.494;
0.470 0.559 0.504 1.752 1.699;
0.665 0.559 0.431 1.483 1.582;
0.800 0.770 0.454 1.757 1.699;
0.663 0.653 0.199 1.223 1.934;
9.904 20.387 13.538 17.383 10.242];
C_p_area_average= [0.110 0.078 0.086 0.857 0.569;
0.126 0.093 0.091 0.976 0.595;
0.116 0.102 0.081 0.858 0.581;
0.120 0.066 0.067 0.527 0.845;
0.116 0.102 0.081 0.858 0.581;
1.058 2.527 6.418 3.411 1.840];
GCp_CC_ASCE=[0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0;
0.3 0.3 0.3 1.0 1.0];
else%if strcmpi(MaxorMin, 'min')
C_p_single_Tap= [-1.836 -2.556 -4.615 -1.827 -2.702;
-2.218 -4.210 -7.497 -2.459 -4.187;
-1.865 -3.626 -6.552 -2.759 -2.480;
-2.541 -4.080 -7.459 -2.948 -4.080;
-1.591 -4.506 -11.288 -2.225 -1.300;
-13.129 -23.544 -39.372 -16.131 -19.103];
C_p_area_average= [-0.918 -0.760 -0.883 -0.926 -0.901;
-0.958 -0.975 -1.396 -0.882 -1.036;
-0.870 -1.014 -0.791 -0.765 -0.893;
-1.142 -0.920 -1.375 -1.045 -1.090;
-0.673 -0.814 -1.472 -0.629 -0.602;
-5.510 -3.513 -6.362 -6.582 -5.826];
GCp_CC_ASCE=[-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4;
-1.7 -2.3 -3.2 -1.1 -1.4];
end %if min or max
clear data
screensize = get( groot, 'Screensize' );
figure('Position', screensize)
t=tiledlayout(1,5,'TileSpacing','none');
for i = 1:5
plot(nexttile,1,C_p_single_Tap(1,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(1,C_p_area_average(1,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(2,C_p_single_Tap(2,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(2,C_p_area_average(2,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(3,C_p_single_Tap(3,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(3,C_p_area_average(3,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(4,C_p_single_Tap(4,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(4,C_p_area_average(4,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(5, C_p_single_Tap(5,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(5,C_p_area_average(5,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
hold on
plot(6,C_p_single_Tap(6,i),'o', 'MarkerFaceColor', [0 0 1],'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
plot(6,C_p_area_average(6,i),'o', 'MarkerFaceColor', [1 0 0.87],'MarkerEdgeColor',...
[1 0 0.87], 'MarkerSize', 14)
yline(GCp_CC_ASCE(:,i),'--','LineWidth',3, 'Color', [1 0 0]);
A=min(min(C_p_single_Tap),min(C_p_area_average));
B=max(max(C_p_single_Tap),max(C_p_area_average));
if strcmpi(MaxorMin, 'max')
text(3,max(B)+1.55, "ZONE " + i ,'FontSize', 26,...
'HorizontalAlignment', 'center', 'VerticalAlignment',...
'middle','Interpreter', 'latex');
else%if strcmpi(MaxorMin, 'min')
text(3,max(B)+1.55, "ZONE " + i ,'FontSize', 26,...
'HorizontalAlignment', 'center', 'VerticalAlignment',...
'middle','Interpreter', 'latex');
end
% ax.XTickLabel= sprintf('Test: %i', Test_Cases{mod(i-1,length(Test_Cases))+1});
end
set(t.Children,'XTick',1:numel(Test_Cases),'XTickLabel',strcat({'Test: '},Test_Cases));
linkaxes(t.Children)
xlim([-1 7])
% xlabel(nexttile(3),'{\rm{Test case number}}','Interpreter','Latex','FontSize', 26);
ylabel(nexttile(1),'${C_p}$','Interpreter', 'Latex','FontSize', 26);
set(t.Children(1:4), 'YTickLabel', [])
rect = [0.90, -0.1, 1, 1];
set(h,'FontSize',26,'Position', rect);
Unrecognized function or variable 'h'.
  3 Comments
Voss
Voss on 24 Jan 2022
Here are some commands you can use/modify at will to set up the xticklabels and yticklabels:
% set up axes like in your code:
t = tiledlayout(1,5,'TileSpacing','none');
nexttile();
nexttile();
nexttile();
nexttile();
nexttile();
linkaxes(t.Children);
xlim([-1 7]);
set(t.Children(1:4),'YTickLabel',[]);
% another way to make Test_Cases, btw:
Test_Cases = arrayfun(@(x)sprintf('%s in %s',x,x),'A':'F','UniformOutput',false);
% adjust all the TickLabels:
% set(t.Children,'XTick',1:numel(Test_Cases),strcat({'Test: '},Test_Cases));
xax = get(t.Children,'XAxis');
set([xax{:}], ...
'FontSize',12, ...
'TickLabelInterpreter','latex', ...
'TickValues',1:numel(Test_Cases), ...
'TickLabel',strcat({'\emph{Test: '},Test_Cases,'}'), ... % \emph{} for italics in latex
'TickLabelRotation',60);
set(get(t.Children(end),'YAxis'), ...
'FontSize',12, ...
'TickLabelInterpreter','latex');

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!