How can i get the same text from another function

  • How can I get the texts, that i have created in the function handles to the function checkboxValue. I want to delete them in the second function. The two functions are not in the same script? Thanks
function handles = plotCAT(vaargin)
text(data(ObstPedestFlag, 1)- 0.5, data(ObstPedestFlag, 2) + 0.5, cellstr('PED-TP'));
text(data(ObstMovInvFlag, 1)- 0.5, data(ObstMovInvFlag, 2) + 0.5, cellstr('INV-TP'));
text(data(ObstStatFlag, 1)- 0.5, data(ObstStatFlag, 2) + 0.5, cellstr('STAT-TP'));
text(data(ObstMovFlag, 1)- 0.5, data(ObstMovFlag, 2) + 0.5, cellstr('MOV-TP'));
end
function checkboxValue= enter_call(hObject, eventdata, handles)
handles = guidata(hObject);
handles.checkboxValue= cell2mat(get(handles.h_checkbox, 'Value'));
handles.checkboxValue
guidata(hObject,handles);
close(gcf)
end

 Accepted Answer

jonas
jonas on 11 Oct 2018
Edited: jonas on 11 Oct 2018
Store their handles in a variable and pass it as output from the first function and input to the second. Alternatively you could use findobj in the second function to grab their handles. If you prefer the latter, then I would set a 'tag' on all the objects that you want to delete, so that you can use:
% First function
text(x,y,'mytext','tag','mytag')
% Second function
delete(findobj(groot,'tag','mytag'))

12 Comments

Hi Jonas, I tried your second way, and it showed me an error:
Undefined function or variable 'root'.
% First function
text(data(ObstStatFlag, 1)- 0.5, data(ObstStatFlag, 2) + 0.5, cellstr('STAT-TP'),'tag','mytag');
%Second function
delete(findobj(root,'tag','mytag'))
oh, change root to 0.
delete(findobj(0,'tag','mytag'))
This is a 'trick' that would work quite well here. But it is probably best if you learn to always store handles to axes and other objects that you intend to access later on. It's just good practice and will save you a lot of time.
"Undefined function or variable 'root'."
root is not a MATLAB function.
For MATLAB versions R2014b and later you can use groot.
For earlier MATLAB versions use 0.
Thanks for your help!:)) I have a last question. Now I want to write a new text at this point. Do I have to get x and y as an output from the first function or can I do this in another way?
My pleasure. I would suggest that you do not delete the text in the first place. Just change the string properties. Here is a little demo of how you can save handles in a cell array and change the strings conveniently.
% Some text objects
ht{1}=text(1,1,'mytext1')
ht{2}=text(1,0.5,'mytext1')
ht{3}=text(1,0,'mytext1')
% Instead of delete
set([ht{:}],'string','');
% Set new strings
newstrings={'mytext3','mytext4','mytext5'};
cellfun(@(x,y)set(x,'string',y),ht,newstrings)
Note that this is easier if you avoid complicating things with a cell array, and just make an array of graphics handles. Both set and get also work with arrays of handles:
H(1) = text(1,1,'one');
H(2) = text(1,0.5,'two');
H(3) = text(1,0,'three');
% Set new strings
C = {'111','222','333'};
set(H(:),{'string'},C(:))
jonas
jonas on 11 Oct 2018
Edited: jonas on 11 Oct 2018
Wow, that worked. Thank you so much :)
I would have never figured out to put the curly braces around the string. This would have helped me many times before.
"I've never figured out how to assign a value to each handle of the array. Do you know an efficient way to do so?"
The second syntax shown in the set help is this: " set(H,NameArray,ValueArray) specifies multiple property values using the cell arrays NameArray and ValueArray. To set n property values on each of m graphics objects, specify ValueArray as an m-by-n cell array, where m = length(H) and n is equal to the number of property names contained in NameArray."
In your case you want to set one property of three objects, so n is one and m is three. This means NameArray should be a 1x1 cell array, and ValueArray should be a 3x1 cell array. So I just put this into code and it works:
H(1) = text(1,1,'one');
H(2) = text(1,0.5,'two');
H(3) = text(1,0,'three');
% Set new strings
C = {'111','222','333'};
set(H(:),{'string'},C(:))
No "figuring out" required, just reading the documentation.
jonas
jonas on 11 Oct 2018
Edited: jonas on 11 Oct 2018
Yes you are right, there is even an example in the doc doing exactly this. In some sense you have to figure out when to read the documentation I suppose ;)
Anyway, this is why I am on this forum, so that more experienced people can correct me when I write crappy code. Thank you for doing so! I am learning a ton.
Hi Stephan. I have tried your solution but it showed me an error:
Undefined function or variable 'c'.
  • first function
c= text(data(ObstStatFlag, 1)- 0.5, data(ObstStatFlag, 2) + 0.5, cellstr('STAT-TP'));
  • second function
A = {'STAT-TN','STAT-FN','Irrelevant'};
set(c,{'string'},A(1))
"I have tried your solution but it showed me an error"
What I wrote was a comment to jonas, it was not a complete answer to your question.
To use it you would still need to pass the variable c from one function to the other. Three hours ago I gave you links that explain how to pass data from one function to another:
Read those links. If you are sensibly writing your own GUI then I recommend using nested functions. If you are unfortunately using GUIDE, then use guidata.
Oh, thanks Stephan! I missed your message to read. I am going to read the links.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2015b

Tags

Asked:

on 11 Oct 2018

Commented:

on 11 Oct 2018

Community Treasure Hunt

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

Start Hunting!