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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 26 Ott 2018
"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 il 26 Ott 2018
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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by