Why can't MATLAB plot the word "factory"?
Asked Answered
H

1

13

Consider the following MATLAB code:

text(0, 0, 'factory');
xlim([-1, 1]);
ylim([-1, 1]);

The intended goal is to have a figure with the word "factory" in it. No word appears. Now replace the word "factory" by any other word and the above code works as intended. This has been tested with MATLAB 2017b and 2015b

Does anyone know what's going on here?

Haws answered 18/4, 2018 at 12:13 Comment(2)
This has been tested with MATLAB 2017b and 2015bHaws
Confirmed on MATLAB 2016b as well.Heteronym
T
13

Problem Explained

According to MATLAB title function documentation:

The words default, factory, and remove are reserved words that will not appear in a title when quoted as a normal character vector. To display any of these words individually, precede them with a backslash, such as '\default' or '\remove'.

This logic apply for text function as well. The Default Property Values page supplies more details about the role of factory keyword and why we can't use it as a parameter for graphic functions.

Solution

The following code works fine:

text(0, 0, '\factory');
xlim([-1, 1]);
ylim([-1, 1]);
Thirtythree answered 18/4, 2018 at 12:24 Comment(2)
another solution to the problem is to wrap the string in a cell array, like this: text(0, 0, {'factory'})Haws
Another way (with R2014b onwards) is t = text(0, 0, ''); t.String = 'factory'. Funnily, t = text(0, 0, ''); set(t, 'String', 'factory') doesn't workLousy

© 2022 - 2024 — McMap. All rights reserved.