Error using string concatenation as input to ordinal( ) function

2 views (last 30 days)
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
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.
Robyn Seery
Robyn Seery on 12 Apr 2021
@the cyclist Apologies, I completely forgot! I have edited the question and code, adding in the data I am using.

Sign in to comment.

Accepted Answer

Cris LaPierre
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)
catT = 1153×2 table
rsrp catRSRP ____ ____________ -115 -115 to -103 -114 -115 to -103 -113 -115 to -103 -113 -115 to -103 -113 -115 to -103 -113 -115 to -103 -112 -115 to -103 -112 -115 to -103 -111 -115 to -103 -111 -115 to -103 -111 -115 to -103 -111 -115 to -103 -111 -115 to -103 -111 -115 to -103 -111 -115 to -103 -110 -115 to -103

More Answers (0)

Categories

Find more on Data Type Identification 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!