classdef and global variables

Suppose I have a file in Matlab where I have my program script. It begins with the declaration and definition of all of the global variables. In some of the other function files, these variables are made known by the line global variable_name;
Suppose that, in addition to function files, I have classdef files. Each classdef is in a separate file.
Suppose a method in one my classes wants to use one of the global variables. How do I use a global statement here? Does it go before the classdef, inside the classdef, and where inside the classdef?
Thanks. Linda Seltzer

4 件のコメント

per isakson
per isakson 2012 年 7 月 24 日
I guess you can include a line
global variable_name
in a method, i.e. after the line
function ...
but I recon it is a really bad idea. It's against everything OOP stands for.
Linda
Linda 2012 年 7 月 24 日
What I am working with was not designed or meant to be object oriented in the real sense of objects and encapsulation.
Malcolm Lidierth
Malcolm Lidierth 2012 年 7 月 25 日
@Linda
@Per's comment is nevertheless correct and answers the question. You might also consider redefining some of those globals as
properties (Constant)
...
where possible which could save save typing but I am not sure in what version TMW introduced that syntax (its post-2008 I think but I'm not sure).
Star Strider
Star Strider 2012 年 7 月 25 日
Would 'Passing Extra Parameters' http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-7.html be of any use to you?

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

2012 年 7 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by