Undefined unary operator '-' for input arguments of type 'string error

1 回表示 (過去 30 日間)
Elnaz P
Elnaz P 2020 年 7 月 7 日
コメント済み: Elnaz P 2020 年 7 月 8 日
I am going to show x axis in scatter plot by text label like below with positive and negative signs however I get this error
"Undefined unary operator '-' for input arguments of type 'string' "
Can someone please assist in rectifying?
n= ["-240-260";"-220-240";"-200-220";"-180-200";"-160-180";"-140-160";"-120-140";"-100-120";"-80-100";"-60-80";"-40-60";"-20- 40";"0-20";"0+20";"+20+40";"+40+60";"+60+80";"+80+100";"+100+120";"+120+140";"+140+160";"+160+180";"+180+200";"+200+220";"+220+240"];
y1 = [0,0,0,0,0,0,0,0,0,0,0,0.04,0.32,0.56,0.08,0,0,0,0,0,0,0,0,0,0];
y2 = [0,0.05,0.05,0.05,0,0.1,0,0,0.2,0.3,0.1,0.1,0.05,0,0,0,0,0,0,0,0,0,0,0,0];
x=categorical(n);
plot(x,y1,'r--o', x,y2,'k--o');
  5 件のコメント
Rik
Rik 2020 年 7 月 7 日
編集済み: Rik 2020 年 7 月 7 日
You have told Matlab those strings are categories, so Matlab treated them as categories. Do you want to treat them as bins of a histogram instead?
Elnaz P
Elnaz P 2020 年 7 月 7 日
Yes it is histogram. I am going to sort x axis from "-260-240" to "+220+240". I did x=sort (categorical(n)); but doesn't work. Please see attached photo.

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2020 年 7 月 7 日
When you call categorical with one input, the list of categories in that categorical is the sorted unique values in the input. From the documentation page:
"B = categorical(A) creates a categorical array from the array A. The categories of B are the sorted unique values from A."
Call categorical with two inputs to keep the categories in your desired order.
"B = categorical(A,valueset) creates one category for each value in valueset. The categories of B are in the same order as the values of valueset"
x = categorical(n, n);
plot(x,y1,'r--o', x,y2,'k--o');
  2 件のコメント
Elnaz P
Elnaz P 2020 年 7 月 8 日
it works, thanks a lot
Elnaz P
Elnaz P 2020 年 7 月 8 日
Hi,
Could I ask another question about change origin point of y axis to (0-20)? I gonna shift Y axis to point (0-20) but I don't have any idea.
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
origin = (0-20);

サインインしてコメントする。

その他の回答 (1 件)

Alan Stevens
Alan Stevens 2020 年 7 月 7 日
Why not just:
y1 = [0,0,0,0,0,0,0,0,0,0,0,0.04,0.32,0.56,0.08,0,0,0,0,0,0,0,0,0,0];
y2 = [0,0.05,0.05,0.05,0,0.1,0,0,0.2,0.3,0.1,0.1,0.05,0,0,0,0,0,0,0,0,0,0,0,0];
x=-250:20:230;
plot(x,y1,'r--o', x,y2,'k--o');
This plots the "y" points in the middle of your "x" bands.
  2 件のコメント
Elnaz P
Elnaz P 2020 年 7 月 7 日
Thanks for your help,
but please consider I have
x= ["-240-260";"-220-240";"-200-220";"-180-200";"-160-180";"-140-160";"-120-140";"-100-120";"-80-100";"-60-80";"-40-60";"-20- 40";"0-20";"0+20";"+20+40";"+40+60";"+60+80";"+80+100";"+100+120";"+120+140";"+140+160";"+160+180";"+180+200";"+200+220";"+220+240"];
not exactly x=-250:20:230;
Alan Stevens
Alan Stevens 2020 年 7 月 7 日
編集済み: Alan Stevens 2020 年 7 月 7 日
Where does it differ?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by