Info

此问题已关闭。 请重新打开它进行编辑或回答。

can setProperties work with save and load?

2 次查看(过去 30 天)
Edward B
Edward B 2019-12-17
关闭: MATLAB Answer Bot 2021-8-20
Any class that is derived from < matlab.System has a problem with required and optional arguments
To illustrate, if you save the class VarArgClass below and run the line above it, it generates the message when loaded.
Apparently, when loading such objects after a save, MATLAB for soem reason creates a temporary instance of the object. However, MATAB apparently neglects to supply the required arguments to the constructor. So without the "try...catch" workaround below, you get an error on load.
Can this be fixed? Or is there a better workaround?
clear; obj = VarArgClass(1, 'propOptional', 2); save; load;
classdef VarArgClass < matlab.System
properties
propRequired
propOptional
end
methods
function self = VarArgClass(propRequired, varargin)
setProperties(self, length(varargin), varargin{:});
try
self.propRequired = propRequired; % required prop not supplied on load
catch
disp('This should only appear in a load');
end
end
end
end

回答(0 个)

此问题已关闭。

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by