Graphic Objects Array Items to Text

9 views (last 30 days)
Jason
Jason on 22 Mar 2023
Answered: Jason on 22 Mar 2023
Hello. I want to view all the graphics objects I have on a UIAxes, and have used the following in an attempt to output all the items to a text area.
clc;
ax=app.UIAxes;
b=ax.Children % b(1) is last object added to uiaxes
class(b(1)) % Check class of 1st item
n=numel(b); % Count of items on UIAxes
for i=1:n
ReportMessage(app,'******* Graphics Objects *******');
% ReportMessage(app,b(i)); %This doesn't work, so try char
ReportMessage(app,char(b(i)));
end
This is my own "ReportMessage" function:
function ReportMessage(app,msg)
currString=get(app.MessagesTextArea,'Value');
%currString=[{char(msg)};currString]; %add to top of message box
currString=[currString; {char(msg)}]; %add to bottom of message box
app.MessagesTextArea.Value=currString;
drawnow;
scroll(app.MessagesTextArea,'bottom');
end
But I'm struggling to convert the graphics item to a string, I haven't found anything searching google.
b =
4×1 graphics array:
Text (\leftarrowy = 0.0000x^2 + -0.0002x + 1.5175)
Line
Line
Line
ans =
'matlab.graphics.primitive.Text'
n =
4
Error using char
Conversion to char from matlab.graphics.primitive.Text is not possible.

Accepted Answer

Jason
Jason on 22 Mar 2023
Heres the full answer:
clc;
ax=app.UIAxes;
b=ax.Children; % b(1) is last object added to uiaxes
n=numel(b);
ReportMessage(app,'******* Graphics Objects [RGB] (1=newest) *******');
for i=1:n
T=b(i).Type; % Get object type (i.e. line, text)
C=b(i).Color; % Get colour of object [R G B] format
ReportMessage(app,[num2str(i),': ',T,', color: ',num2str(C)]);
end
drawnow;

More Answers (1)

Mario Malic
Mario Malic on 22 Mar 2023
Hello,
Text is a component(object) and it has String property which contains the actual text. Keep in mind that b is an array and you will have to get the text component only as other elements do not have the same properties. Correct way in your case should be
b(1).String
You can also use findall function just to find objects of particular type in your graphic objects.
  2 Comments
Jason
Jason on 22 Mar 2023
Hi, thanks for your post. So I have tried your suggestion:
clc;
ax=app.UIAxes;
b=ax.Children % b(1) is last object added to uiaxes
class(b(1))
n=numel(b);
for i=1:n
ReportMessage(app,'******* Graphics Objects *******');
ReportMessage(app,b(i).String);
end
But get the following:
Unrecognized method, property, or field 'String' for class 'matlab.graphics.chart.primitive.Line'.
Jason
Jason on 22 Mar 2023
It looks like "Type" is what I need i.e.
ReportMessage(app,b(i).Type);

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!