I'm new to MATLAB and really struggling with their datatypes / conventions compared to other programming languages.
For instance, I have created a simple plot (e.g. using the peaks command) and simply want to include a padding space before all xticklabels. My MATLAB/pseudocode solution is thus:
labels = xticklabels; # Get labels
newlabels = xticklabels; # Create new array
i = 1
for label in labels # Loop through all labels
label = ' ' + label # Add single character pad
newlabels(i) = label # Update new labels array
i = i + 1
set(gca,'XTickLabel', {newlabels}) # Set plot to use new array
How can I achieve this please? I feel like it should be possible quite simply
Thanks!
PS, I have found the pad command in MATLAB2017, but not all my xticklabels are equal length and hence, I only want to add one trailing space, not fix the total string length using pad
The simplest way, given a cell array of strings, is to use strcat:
labels = {'1','2','3','4'};
newlabels = strcat('x',labels); % append 'x' because it's more visible
Result:
newlabels =
{
[1,1] = x1
[1,2] = x2
[1,3] = x3
[1,4] = x4
}
Alternatively, you could loop through the cell array and concatenate to each char array:
newlabels = cell(size(labels)); % preallocate cell array
for k = 1:numel(labels)
newlabels{k} = ['x', labels{k}]; % concatenate new char to existing label
end