I created a class based on a list of variables that comes from data. I made a function that will place all data inside an object of this class.
As the list of variables grows, I keep having to adjust the class and the load function. I am trying to find a way that will make my load function robust against changes in the class.
Code of the Class file:
classdef CoolClass
properties
prop_a
prop_b
prop_c
end
end
The function I wrote to load this data into an object:
function [ object_name ] = loadCoolClass (data_file_name)
load(data_file_name)
% this loads the variabels:
% prop_a = 1, prop_b = 2, prop_c = 3, prop_d = 4
object_name = CoolClass;
object_name.prop_a = prop_a;
object_name.prop_b = prop_b;
object_name.prop_c = prop_c;
end
Now what I would like to have is something that if I add a variable prop_d
to the classdef file that it will immediately load it into the loadfile as well. something like this:
function [ object_name ] = loadCoolClass (data_file_name)
load(data_file_name)
% this loads the variabels:
% prop_a = 1, prop_b = 2, prop_c = 3, prop_d = 4
object_name = CoolClass
cool_properties = properties(CoolClass)
for i = 1:size(cool_properties,2)
object_name.cool_property(i) = cool_property(i)
end
end
Now I know the loop above is not valid code but I mean: check for every property if there is a variable with that name and place it in the object.
Is there a way in matlab to use a variable holding a string, as an input to load the value of the variable named after the content of the string?
Is this possible in naming content before the '=' sign Is this possible in referring to variables after the '=' sign