Change string of static text boxes whose tag contains "Output"

5 views (last 30 days)
Hi all,
I am writing some analysis software in 2015a with GUIDE.
I am looking to push a button that will reset the GUI to how it opened, emptying (by setting string) ALL edit boxes (easy) and all static text fields that are used as an output. I do not want to empty static text boxes whose tag contains "Text" as these are GUI labels next to edit boxes.
Currently I have the code below, but I don't know how to adapt it for the above...
% Empty All Static Text Boxes Whose Tag Contains "Output"
set(findall(handles.figure1,'Tag','Output'),'string',''); % findall finds invisible objects as well (findobj does not)
My previous attempt was:
% Get All Static Text Tags
AllStaticTextTags = findall(handles.figure1,'style','text')
% Find All Static Text Tags Containing "Output"
StaticTextTagsContainingOutputIdx = contains(AllStaticTextTags,'Output'); % returns 1 if contains it 0 if not
StaticTextTagsContainingOutput = AllStaticTextTags(StaticTextTagsContainingOutputIdx);
% Empty All Static Text Boxes Whose Tag Contains "Output"
set(StaticTextTagsContainingOutput,'string','');
But understandably that doesn't work due to:
Undefined function 'contains' for input arguments of type 'matlab.ui.control.UIControl'.
Currently I am closing the window and re-running the main script but this is slow and inefficient.
EDIT: To clarify, the tags contain other letters also... I am looking to ONLY empty strings of static text boxes that have "Output" in their tag name.
Thanks,
Matt.
  3 Comments
Matt
Matt on 10 Jul 2018
Does that not do the same as this?
set(findall(handles.figure1,'Tag','Output'),'string','');
The problem with that is that the tags aren't just "Output". e.g. "CalculationOfSpringRateOutput" so I need to be able to empty the strings of ONLY the static text boxes that contain "Output". Some static text boxes are tagged "DataEntry1TextLabel" for example.
Adam
Adam on 10 Jul 2018
Edited: Adam on 10 Jul 2018
The handles of all your GUI components are stored directly on the handles struct. I would have thought it would be faster to interrogate its fields using something like
names = fieldnames( handles );
idx = contains( fieldnames( handles ), 'Output' );
names = names( idx );
for n = 1:numel( names )
handles.( names{ n } ).String = [];
end
That is off the top of my head and may contain errors.
Using findall seems very inefficient for a self-contained GUI and seems a very brittle way of doing things though. Personally I would keep an array of the relevant text handles that I populate at the creation of the UI if I were to do this. Then setting them to empty would be trivial.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 10 Jul 2018
Matt - the function contains looks to have been introduced in r2016b so that function is most not likely available to you (given that your version is r2015a).
You can try the following though. Continue as before to get all the handles to the static text controls
allStaticTextHandles = findall(handles.figure1,'style','text');
Then get the tags for each one (note that I'm using code for r2014a so it may be different for your version)
allStaticTextTags = get(allStaticTextHandles, 'Tag');
And now you could use strfind to find those tags that contain the string 'Output'. The result of
strfind(allStaticTextTags, 'Output')
will be a cell array of empty arrays or indices of where the string 'Output' starts in the tag. For example,
ans = { []; [5]; []}
and so you would need to find which of these is non-empty and then clear the contents of that static text control.
  4 Comments
Geoff Hayes
Geoff Hayes on 10 Jul 2018
Matt - something like this worked for me
allStaticTextControlHandles = findobj(handles.figure1,'style','text')
staticTextControldToClear = cell2mat(cellfun(@(x)~isempty(x),strfind(get(allStaticTextControlHandles,'Tag'),'Output'), 'UniformOutput',false));
set(allStaticTextControlHandles(staticTextControldToClear),'String','');
Matt
Matt on 3 Sep 2018
Sorry for the late response. Not sure why I used a for loop in my solution - useless! Your solution is perfect, and much neater - thank you Geoff, I couldn't have solved this problem without your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!