Prev: mean ,variance,standard deviation ,kurtosis of image
Next: Second order sensitivity coefficients
From: John D'Errico on 23 Jul 2010 08:32 "bjorn.tor.larsson(a)gmail.com" <bjorn.tor.larsson(a)gmail.com> wrote in message <95126884.41137.1279877740041.JavaMail.root(a)gallium.mathforum.org>... > As far as I understand, in order to make variables global, they need to be stated with "global variablename" in each and every function where they should be available. Is there another way? > > Maybe I have a poor programming style, but I need lots of variables available in lots of functions. Most of my coding time is spent on adding variable names to those global lists in all functions. It can't be meant to be like this. > > Thanks for any help beforehand! Well, perhaps this will teach you a lesson. Don't use them. Global variables are a crutch, a way to write terribly lazy code. They cause you to do exactly as you have done, create a messy tangle of globals. They make your code more difficult to debug. Of course, your response will be that you simply cannot undo this use of global variables, that your code is now too complex to change. So why are you asking for help? I would have recommended, BEFORE you wrote this miserable tangle of code, to learn to use a single struct to contain all of your parameters in one place. Then pass it down as an argument into the depths of your functions. (If you are worried about memory problems, MATLAB does not make copies of that which does not change.) In some cases, simply learning to use function handles to pass in arguments to functions can avoid use of globals too. So you can clean up your code, or you can choose to live in the mess you have made. We cannot help you unless you are willing to improve this poor programming style. There is no magic fix. John
From: Steven_Lord on 23 Jul 2010 09:18 <bjorn.tor.larsson(a)gmail.com> wrote in message news:95126884.41137.1279877740041.JavaMail.root(a)gallium.mathforum.org... > As far as I understand, in order to make variables global, they need to be > stated with "global variablename" in each and every function where they > should be available. Is there another way? Is there another way to use global variables? No. Is there another way to accomplish the task for which you're currently using global variables? Perhaps. > Maybe I have a poor programming style, but I need lots of variables > available in lots of functions. Most of my coding time is spent on adding > variable names to those global lists in all functions. It can't be meant > to be like this. Then I recommend storing those variables as fields in a struct array and passing the struct into your "lots of functions". Then you can extract only those fields you need in your function. If that's not an option, explain a little more about what your goal is and perhaps someone can offer a suggestion other than global variables. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: BjornL on 23 Jul 2010 06:49 Thanks all! By using handles and data structures might make it more convenient to call functions with many arguments. However, I can't make the syntax work: To reduce argument lists, I found this example: function h = get_plot_handle(lnSpec, lnWidth, mkEdge, ... mkFace, mkSize) h = @draw_plot; function draw_plot(x, y) plot(x, y, lnSpec, ... 'LineWidth', lnWidth, ... 'MarkerEdgeColor', mkEdge, ... 'MarkerFaceColor', mkFace, ... 'MarkerSize', mkSize) end end But when I try something simple like: function s = addingConstruct(a, b, c) s = @adding; function q = adding(d) q = a+b+c+d; end end And then type first addingConstruct(1,2,3) and then s(4), I get "undefined function or method 's'". Trying to decipher how to use a strange phenomena like "handles" wastes so much time! It's like starting to build a car instead of taking the bus.
From: someone on 23 Jul 2010 11:45 BjornL <bjorn.tor.larsson(a)gmail.com> wrote in message <2126029284.43143.1279896613586.JavaMail.root(a)gallium.mathforum.org>... > Thanks all! > > By using handles and data structures might make it more convenient to call functions with many arguments. However, I can't make the syntax work: > > > To reduce argument lists, I found this example: > > function h = get_plot_handle(lnSpec, lnWidth, mkEdge, ... > mkFace, mkSize) > h = @draw_plot; > function draw_plot(x, y) > plot(x, y, lnSpec, ... > 'LineWidth', lnWidth, ... > 'MarkerEdgeColor', mkEdge, ... > 'MarkerFaceColor', mkFace, ... > 'MarkerSize', mkSize) > end > end > > > But when I try something simple like: > > function s = addingConstruct(a, b, c) > s = @adding; > function q = adding(d) > q = a+b+c+d; > end > end > > And then type first addingConstruct(1,2,3) and then s(4), I get "undefined function or method 's'". > > Trying to decipher how to use a strange phenomena like "handles" wastes so much time! It's like starting to build a car instead of taking the bus. Yes, but once you learn how to build a car and operate it, its a lot more convienent to drive a car than take the bus. (Green issues aside.)
First
|
Prev
|
Pages: 1 2 Prev: mean ,variance,standard deviation ,kurtosis of image Next: Second order sensitivity coefficients |