Remove all doubles, cell arrays and char arrays from a structure?

5 次查看(过去 30 天)
I am writing a piece of software with a GUIDE interface.
My data is all stored in the handles structure. In hindsight, I should have put every variable the user enters, and every result of calculations in a seperate structure like UserData.XXX rather than handles.XXX but it's a very large interface and I am working to a tight time constraint so can't easily change this now.
When the user opens a new GUI, I clear all of the edit boxes and result outputs whose tag names end in INPUT or OUTPUT. It's fast and works well, but the data remains in the handles structure. When a computation button is pressed again, naturally it still works, as the data is still present even if the GUI is empty.
How can I remove fields from the handles structure if (and only if) they are character arrays, doubles, or cell arrays? That way my GUI UIControl data, panels etc. can remain in the handles structure.
I was hoping to easily use the class and rmfield functions, but I can only get the class function to identify that handles is a struct, rather than return the class of each item contained within it.
Thanks, Matt.
  2 个评论
Stephen23
Stephen23 2018-10-26
"In hindsight, I should have put every variable the user enters, and every result of calculations in a seperate structure like UserData.XXX rather than handles.XXX but it's a very large interface and I am working to a tight time constraint so can't easily change this now."
That might be your best option. I would simply replace your handles.XXX with handles.mystuff.XXX, which could be done with a simple text find-and-replace, although you have to check each one so that you do not change the GUIDE fields, which will take a few minutes. After that, you can process/replace/initialize/... the contents of handles.mystuff as you require.
Matt
Matt 2018-10-26
I think you are right... It will take me quite some time to do though, as there's a hell of a lot of data entries and calculations.
I have worked around it for now, but I think i'll do the above when my tight deadline has passed.
% Empty all edit boxes and static text boxes
% if their tag names end in input or output
ResetDefaultGUI
% Get all fieldnames in handles structure
HandlesFieldNames = fieldnames(handles);
% Find the fields below using a breakpoint here.
% Copy in the variable viewer. They copy in as a cell array by default)
FieldNamesNotToRemove = {'figure1';'NewButton';'LoadButton';'SaveButton';...
'etc. * hundreds'}
% Find the fieldnames to remove
FieldNamesToRemove = setdiff(HandlesFieldNames,FieldNamesNotToRemove);
% Remove the fields
handles = rmfield(handles,FieldNamesToRemove);
% Update handles structure
guidata(hObject, handles);
It just means that in the future when I add any components to my GUI I need to remember to update FieldsNotToRemove.
Any better temporary solutions would be greatly appreciated.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by