user2041602
user2041602

Reputation: 179

Matlab label names won't work when decimals are in the names

I am trying to set the y axis to have ticks every 0.25 points from 0-4 and have labels on those ticks on 0,0.5,1,1.5,...

This code works when I do not have any decimal points in my numbers but fails with error once I add in 0.5 and 1.5 etc.

set(gca,'YTick',(0:.25:4), 'FontSize', 13)
set(gca,'YTickLabel',['0';' ';'0.5';' ';'1';' ';'1.5';' ';'2';' ';'2.5';' ';'3';' ';'3.5';' ';'4'])

This is the matlab error message:

Error using generateWiresharkTracePlot (line 63)
Error using vertcat
Dimensions of matrices being concatenated are not consistent.

Does anyone have an idea for a solution?

Upvotes: 1

Views: 129

Answers (1)

Shai
Shai

Reputation: 114786

The problem:
You are building a 2D char matrix using

['0';' ';'0.5';' ';'1';' ';'1.5';' ';'2';' ';'2.5';' ';'3';' ';'3.5';' ';'4']

You are trying to define rows with 1 char (e.g., '0') and rows with 3 chars (e.g., '1.5').

Solution 1:

Convert all rows to thress chars:

[' 0 ';'   ';'0.5';'   ';' 1 ';' ';'1.5';'   ';' 2 ';'   ';'2.5';'   ';' 3 ';'   ';'3.5';' ';' 4 ']

Solution 2:

Use cell array instead of 2D char matrix (Note the curly braces):

{'0';' ';'0.5';' ';'1';' ';'1.5';' ';'2';' ';'2.5';' ';'3';' ';'3.5';' ';'4'}

This solution is better in terms of generalization and good practice.

Upvotes: 3

Related Questions