Color in plot from Table

11 ビュー (過去 30 日間)
MattC
MattC 2023 年 3 月 22 日
コメント済み: Les Beckham 2023 年 3 月 26 日
Hi All, I have a table in matlab which has some columns which I trying to plot. The table also has a column for the color which I want to use as color point but when I try and use this column directly to plot the data it gives me an error. Can someone help how to plot this?
xaxis = [1,2];
A = [10,15];
B = [45,65];
Color = ["#808080","#808080"];
Table1 = table (xaxis,A,B,Color);
Trying to plot it like:
Odds = [];
Odds(end+1) = plot(Table1.xaxis,Table1.A,'s','MarkerFaceColor','none','MarkerEdgeColor',sprintf('%s',Table1.Color));
hold on
Odds(end+1) = plot(Table1.xaxis,Table1.B,'+','MarkerFaceColor','none','MarkerEdgeColor',sprintf('%s',Table1.Color));
% This is the error I receive
% Error using plot
% Invalid color name or hexadecimal color code. Valid names include: 'red', 'green', 'blue', 'cyan',
% 'magenta', 'yellow', 'black', 'white', and 'none'. Valid hexadecimal color codes consist of '#' followed
% by three or six hexadecimal digits.

回答 (1 件)

Les Beckham
Les Beckham 2023 年 3 月 22 日
Just a couple of issues. You need column vectors to insert into the table, and you can only have a scalar string or char vector for specifying the MarkerEdgeColor using a hex color code.
xaxis = [1,2]'; % <<< transpose these to get column vectors to use in creating the table
A = [10,15]';
B = [45,65]';
Color = ["#808080","#808080"]';
Table1 = table (xaxis,A,B,Color)
Table1 = 2×4 table
xaxis A B Color _____ __ __ _________ 1 10 45 "#808080" 2 15 65 "#808080"
Odds = [];
Odds(end+1) = plot(Table1.xaxis,Table1.A,'s','MarkerFaceColor','none','MarkerEdgeColor',Table1.Color(1));
hold on
Odds(end+1) = plot(Table1.xaxis,Table1.B,'+','MarkerFaceColor','none','MarkerEdgeColor',Table1.Color(1));
xlim([0 3])
ylim([5 70])
grid on
  10 件のコメント
MattC
MattC 2023 年 3 月 25 日
The point is to plot the data from the table and I think I shared everything I had already. I know it is easier to just plot it directly but that is not what I am have to do
Les Beckham
Les Beckham 2023 年 3 月 26 日
I'm pretty sure that I have shown you all of the available options for using the plot command to specify markers and colors, and how to extract data from a table. If you won't share your actual data and the code that you are using (and why, specifically, it doesn't do what you want), I can't help you any further.
Experiment with changing the code that you have (using the suggestions I gave you) until you get what you want. That is the thing that is great about Matlab, it is easy to try things and see what happens, and then change it until it does what you want.

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

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by