필터 지우기
필터 지우기

Error using string concatenation as input to ordinal( ) function

조회 수: 2 (최근 30일)
Robyn Seery
Robyn Seery 2021년 4월 12일
댓글: Robyn Seery 2021년 4월 12일
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
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.
Robyn Seery
Robyn Seery 2021년 4월 12일
@the cyclist Apologies, I completely forgot! I have edited the question and code, adding in the data I am using.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
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)
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
  댓글 수: 1
Robyn Seery
Robyn Seery 2021년 4월 12일
Thank you very much, works just as I needed it to! Really appreciate your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by