How to pass a class to MATLAB executable

10 vues (au cours des 30 derniers jours)
Deepa Maheshvare
Deepa Maheshvare le 20 Août 2021
Commenté : Walter Roberson le 23 Août 2021
This is a follow up to my previous here.
I have the following script
main.m >>
function m = magicsquare
n = setting.n;
if ischar(n)
n=str2double(n);
end
m = magic(n);
disp(m)
and a class from which the variable n is read by the above script.
classdef setting
%SETTING Summary of this class goes here
% Detailed explanation goes here
properties(Constant)
n = 5;
end
% methods
% function obj = setting(inputArg1,inputArg2)
% %SETTING Construct an instance of this class
% % Detailed explanation goes here
% obj.Property1 = inputArg1 + inputArg2;
% end
%
% function outputArg = method1(obj,inputArg)
% %METHOD1 Summary of this method goes here
% % Detailed explanation goes here
% outputArg = obj.Property1 + inputArg;
% end
% end
end
I have converted main.m into a executable using the Application compiler. Since I want the user to specify input in `setting` class, this class was not converted into a binary during compilation (i.e. I have excluded the setting.m file from `Files required for your application to run` tab in the Application compiler.)
Here is what I tried: I simply saved main.exe and setting.m class in the same folder and ran !main.
I get the following error
Unable to resolve the name setting.n.
Error in main (line 2)
MATLAB:undefinedVarOrClass
I could have passed `n` as an input argument to the executable like mentioned in my previous post since `n` is a single property defined in setting class. However for my real system , there are more properties defined in settings and I want the user to define all the properties in a single class file from which the rest of functions can access the property values defined. I am not sure how to pass a class to the `main` executable. Suggestions will be really helpful.

Réponse acceptée

Walter Roberson
Walter Roberson le 20 Août 2021
You cannot do that.
Any .m or .p file (script, function, class definition) that is not completely defined at the time of compilation, would have to be interpreted at run-time of the executable. However, compiled executables have no ability to interpret MATLAB.
If you want the user to be able to provide settings in a file, then you will need to have them do that in the form of a data file that your program analyzes to set up its variables.
  8 commentaires
Deepa Maheshvare
Deepa Maheshvare le 23 Août 2021
Modifié(e) : Walter Roberson le 23 Août 2021
I magaged to read structs from initialization files using [this]( https://in.mathworks.com/matlabcentral/fileexchange/55766-ini ) to set up the following
Now, I've the following class
classdef setting
%SETTING Summary of this class goes here
% Detailed explanation goes here
properties(Constant)
n = setting.get_n;
q = setting.get_q;
end
methods(Static)
function data = load_setting
File = 'Configuration.ini';
I = INI('File',File);
I.read();
data = I.get('UserData'); % struct
end
function n = get_n
n = data.m + 2;
end
function q = get_q
temp = data.q;
q = fieldnames(q);
end
end
end
I'm looking for a way to easily access `data` returned by load_setting in the rest of the methods defined in the class. Suggestions will be really helpful.
Walter Roberson
Walter Roberson le 23 Août 2021
temp = data.q;
q = fieldnames(q);
You do not do anything with temp and then in the next line you use the undefined variable q
Your code is assuming that the ini file is giving back properties m and q but you should not be assuming that. You should be validating every necessary property (and for any optional properties you should be checking that they exist when the user asks for them.)
classdef setting
Class name is setting
methods(Static)
function data = load_setting
Method name is something different than setting, so that method is not the class constructor.
function n = get_n
n = data.m + 2;
end
data is not a property of the class, and does not appear to be the name of a class or package. data is probably not defined in that context.
function data = load_setting
File = 'Configuration.ini';
I = INI('File',File);
I.read();
data = I.get('UserData'); % struct
end
That's a static class, and besides setting is not a handle class, so load_setting is going to return the values to whichever function calls it. You never call the function and store the results anywhere, so the data local variable there is disconnected from the use of data in your two get_* functions.
properties(Constant)
n = setting.get_n;
q = setting.get_q;
end
Okay, you are defining constants, and it looks like you intend to call methods of your class in order to do so. So, exactly when are those methods going to be called?
MATLAB evaluates the expressions when loading the class.
Your get_n and get_q expect data to have been constructed already, which implies that they expect load_setting to be called already. But load_setting does not get called automatically, since it is just an ordinary method.
If the content of load_setting was the constructor method, then there would be the issue that constructor methods are called when an instance is created, not when the class is loaded. At the moment I do not find any special method that are triggered when the class is loaded, but I am not at all experienced in that topic.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by