-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #356 Completed (not tested) serializable class
- Loading branch information
Showing
4 changed files
with
66 additions
and
78 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
herbert_core/utilities/classes/@serializable/from_struct.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function obj = from_struct(obj,inputs) | ||
% Restore object from the fields, previously obtained by struct method | ||
% | ||
struct_fields = fieldnames(inputs); | ||
if numel(inputs)>1 | ||
obj = repmat(obj,numel(inputs)); | ||
for i=1:numel(inputs) | ||
obj(i) = set_obj(obj(i),inputs(i),struct_fields); | ||
end | ||
obj = reshape(obj,size(inputs)); | ||
else | ||
obj = set_obj(obj,inputs,struct_fields); | ||
end | ||
% | ||
function obj = set_obj(obj,inputs,flds) | ||
% | ||
for i=1:numel(flds) | ||
fld = flds{i}; | ||
obj.(fld) = inputs.(fld); | ||
end |
64 changes: 14 additions & 50 deletions
64
herbert_core/utilities/classes/@serializable/private/loadobj_.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,25 @@ | ||
function obj = loadobj_(S,class_instance) | ||
% Restore object from the data as they are stord on a hard drive | ||
% | ||
% Input: | ||
% S -- the structure, | ||
% | ||
% Output: | ||
% ------- | ||
% obj An instance of class_instance object or array of objects | ||
% | ||
ver_requeste = class_instance. | ||
if isstruct(S) | ||
obj = PixelData(); | ||
if isfield(S,'version') | ||
% load PixelData objects, written when saveobj method was | ||
% written | ||
if S.version == 1 | ||
if isfield(S,'array_data') % multidimensional array of pixel data | ||
S = S.array_data; | ||
obj = repmat(obj,size(S)); | ||
for i=1:numel(S) | ||
if i>1 % cloning handle object as repmat makes the handles | ||
% identical | ||
obj(i) = PixelData(); | ||
end | ||
obj(i).set_data('all',S(i).data); | ||
obj(i).set_range(S(i).pix_range); | ||
obj(i).file_path_ = S(i).file_path; | ||
end | ||
else % Single object | ||
obj.set_data('all',S.data); | ||
obj.set_range(S.pix_range); | ||
obj.file_path_ = S.file_path; | ||
end | ||
ver_requested = class_instance.classVersion(); | ||
if isfield(S,'version') | ||
if S.version == ver_requested | ||
S = rmfield(S,'version'); | ||
if isfield(S,'array_data') | ||
obj = from_struct(class_instance,S.array_data); | ||
else | ||
error('HORACE:PixelData:invalid_argument',... | ||
'Unknown PixelData input structire version'); | ||
obj = from_struct(class_instance,S); | ||
end | ||
else % previous version(s), written without version info | ||
if isfield(S,'data_') | ||
for i=1:numel(S) | ||
set_data(obj(i),'all',S(i).data_); | ||
obj(i).reset_changed_coord_range('coordinates') | ||
end | ||
elseif isfield(S,'raw_data_') | ||
for i=1:numel(S) | ||
obj(i).set_data('all',S(i).raw_data_); | ||
obj(i).set_range(S(i).pix_range_); | ||
obj(i).file_path_ = S(i).file_path_; | ||
end | ||
else | ||
error('HORACE:PixelData:invalid_argument',... | ||
'Unknown PixelData input structire version'); | ||
end | ||
end | ||
else | ||
if isempty(S.page_memory_size_) | ||
% This if statement allows us to load old PixelData objects that | ||
% were saved in .mat files that do not have the 'page_memory_size_' | ||
% property | ||
S.page_memory_size_ = PixelData.DEFAULT_PAGE_SIZE; | ||
else | ||
obj = from_old_struct(class_instance,S); | ||
end | ||
obj = PixelData(S); | ||
else % previous version(s), written without version info | ||
obj = from_old_struct(S); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters