Error using string concatenation as input to ordinal( ) function
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
the cyclist
2021-4-12
It would be helpful if you uploaded the data variable in a MAT file, so that we can see exactly what you are.
采纳的回答
Cris LaPierre
2021-4-12
编辑:Cris LaPierre
2021-4-12
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!