How to load a structure as a handle?

27 Ansichten (letzte 30 Tage)
Ibro Tutic
Ibro Tutic am 1 Jun. 2017
Beantwortet: E Wilder am 9 Dez. 2018
I am trying to load a structure to a handle that I can use in a GUI. When I load the structure, I use
handles.data=load('structure.mat')
However, when I want to pull data from it, I have to use a nested structure of sorts. So if the original structure was DLG, when using the data within the GUI, I have to use handles.data.DLG.fieldname. Is there anyway to just use the DLG structure instead of all of the handles stuff? Below is a function that might be relevant to the question.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.data = load(fullfile(handles.folder, handles.item));
tmp_nam = fieldnames(handles.data.DLG.metadata);
tmp_val = cellfun(@(x)(handles.data.DLG.metadata.(x)),tmp_nam,'UniformOutput',false);
handles.table = [tmp_nam tmp_val];
set(handles.uitable2,'data',handles.table);
guidata(gcbo, handles);
  1 Kommentar
Jan
Jan am 1 Jun. 2017
Bearbeitet: Jan am 1 Jun. 2017
No, this does not "load data to a handle". The struct is called "handles", but it could have any other name also. A "handle" is something very specific. You load the contents of the file to a field of the struct "handles".
I do not know what "a nested structure of sorts" is. "all of the handles stuff" is not clear also.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 1 Jun. 2017
Bearbeitet: Jan am 1 Jun. 2017
FileData = load('structure.mat');
handles.Dlg = FileData.Dlg;
I'm not sure, but perhaps tmp_val can be obtained easier:
tmp_val = struct2cell(FileData.Dlg);

Weitere Antworten (1)

E Wilder
E Wilder am 9 Dez. 2018
Daniel Shrub provides a good answer to this here:
https://www.mathworks.com/matlabcentral/answers/46884-is-there-something-like-a-struct-pointer-in-matlab

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by