Error using string concatenation as input to ordinal( ) function
2 views (last 30 days)
Show older comments
Robyn Seery
on 12 Apr 2021
Commented: Robyn Seery
on 12 Apr 2021
Hi, I am trying to create dynamic legend titles, based on numeric input ranges (RSRP signal levels). Given input sorted_RSRP, I am adding a category column, to specify the signal ranges for plotting colour-coded map data.
To do this, I have attempted to combine string values in the ordinal( ) function so I can display the ranges (that would change for different data sets) in the legend. I get the following error:
Error using categorical (line 391)
CATEGORYNAMES must be a string array or cell array of character vectors containing non-blank category names.
Error in ordinal (line 148)
b = b@categorical(a,args{:},'Ordinal',true);
See code below:
% five data categories
p = 0:0.2:1;
breaks = quantile(rsrp,p)
% string concatenation to show numeric range values
a = num2str(breaks(1)) + " to " + num2str(breaks(2))
b = num2str(breaks(2)) + " to " + num2str(breaks(3))
c = num2str(breaks(3)) + " to " + num2str(breaks(4))
d = num2str(breaks(4)) + " to " + num2str(breaks(5))
e = "> " + num2str(breaks(5))
% categorise RSRP
catRSRP = ordinal(rsrp, {a,b,c,d,e}, [], breaks);
% I have also tried this, inputting string concatenation directly
% catRSRP = ordinal(data_sorted.RSRP, {num2str(breaks(1)) + " to " + num2str(breaks(2)), ...
% num2str(breaks(2)) + " to " + num2str(breaks(3)), ...
% num2str(breaks(3)) + " to " + num2str(breaks(4)), ...
% num2str(breaks(4)) + " to " + num2str(breaks(5)), ...
% "> " + num2str(breaks(5))}, [], breaks);
Not entirely sure what the error I am getting means, none of the a, b, c, d, e string variables are empty.
Any help would be greatly appreciated!
2 Comments
the cyclist
on 12 Apr 2021
It would be helpful if you uploaded the data variable in a MAT file, so that we can see exactly what you are.
Accepted Answer
Cris LaPierre
on 12 Apr 2021
Edited: Cris LaPierre
on 12 Apr 2021
There is a warning at the top of the documentation page for ordinal
- The nominal and ordinal array data types are not recommended. To represent ordered and unordered discrete, nonnumeric data, use the Categorical Arrays data type instead.
I think what you want to use is discretize to assign categories to the values based on what bin they would fall into. Use the syntax
load rsrp.mat
% five data categories
p = 0:0.2:1;
breaks = quantile(rsrp,p);
% string concatenation to show numeric range values
a = breaks(1) + " to " + breaks(2);
b = breaks(2) + " to " + breaks(3);
c = breaks(3) + " to " + breaks(4);
d = breaks(4) + " to " + breaks(5);
e = "> " + breaks(5);
% categorise RSRP
catRSRP = discretize(rsrp,breaks,'categorical',[a,b,c,d,e]);
catT = table(rsrp,catRSRP)
More Answers (0)
See Also
Categories
Find more on Categorical Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!