0

I operate on global variable the way below in code. I want to save every time to global variable and keep its content so that it expands. If I declare function with output being this variable, in case of huge structures it may significantly slow down I assume. How to do it?

function test()

    global n1;
    n1 = [1 2 3];

    for x=1:10
%     [n1] = global_up(n1,x); % no need for output parameter, as n1 is global right?
      f_up(n1,x);
    end 
end

function f_up(arg1,arg2) %function [arg1] = f_up(arg1,arg2) is wrong?
    global n1; % need to write it in every function ?
    arg1 = [arg1 arg2];
end
berndh
  • 1,355
  • 4
  • 14
  • 19
  • 1
    What is the purpose of the function f_up? I don't see where n1 is manipulated in your function. Also, matlab is `call by value`, which means that arg1 and arg2 are copied during the function call and your manipulation on arg1 will not affect the object that you used as an argument in your function call. You should clarify your question maybe? – Konstantin Schubert Mar 20 '13 at 13:26
  • Ah, ok. If the variable f_up, is global, I think you don't have to pass it. So simply writing `n1 = [ n1 arg2 ]` should do the trick. (I have never tried it in matlab, though.) But, please, try to avoid it. This question is the same that you have, and it is well answered: http://stackoverflow.com/questions/4137905/how-to-modify-an-array-in-function – Konstantin Schubert Mar 20 '13 at 14:43
  • This question is the same that you have, and it is well answered: http://stackoverflow.com/questions/4137905/how-to-modify-an-array-in-function – Konstantin Schubert Mar 20 '13 at 14:49
  • thank you i didnot find it. this solution works, and i realize it is not optimal :) – berndh Mar 20 '13 at 15:54
  • @Konstantin If I recall correctly, Matlab uses something called copy on write. That means that Matlab does no copy any variable passed unless you modifies it. In `f_up` it would mean that `arg 1` is copied, but not `arg2`. – patrik May 12 '15 at 22:01

2 Answers2

1

I don't really understand what you are trying to do, but global variables are almost definitely not the way to go. just pass the variable as a parameter:

   function test()

        n1 = [1 2 3];

        for x=1:10
          n1 = f_up(n1,x);
        end 
    end

function arg3 = f_up(arg1,arg2)
    arg3 = [arg1 arg2];
end

But if you explain the point of this code you might get a much better solution. as it stand f_up really doesn't do anything (i.e. it encapsulates syntax that is already very concise with no benefit)

Dan
  • 45,079
  • 17
  • 88
  • 157
  • functions are simplified to show concept. They will be more complicated. My question is how should i write into global variable inside f_up function, to have this data available in test() and later also in function that calls test() etc. in higher instances. Either can I create a normal variable and pass as output, or do with global, how to do the 2nd option? – berndh Mar 20 '13 at 13:27
  • 2
    My answer is don't. Pass it out like I have. Global variables are not a great design, you should be just passing variables in and out. – Dan Mar 20 '13 at 13:33
  • ok so function [arg1] = do_it(arg1, arg2) is for me to read from arg2 and update arg1. And do this every time I go "higher" in functions. Correct please if I got it wrong. And could you give me an answer to how to do it with global variable anyway? – berndh Mar 20 '13 at 13:39
  • 2
    Just declare the variable as `global` **everywhere** you want to use it and then treat it like a normal variable (but bear in mind the changes will propagate to the rest of the code where it is declared as `global`). `[arg1] = do_it(arg1, arg2)` is a good way to go, as far as I know that's how all the solvers etc work for example – Dan Mar 20 '13 at 13:48
  • @berndh also, note that if some colleague modifies the variable in some of his functions (presumably for a good reason) you may get a shock after the next update... – patrik May 12 '15 at 22:04
0

EDIT: Consider this question for a discussion of your actual problem: How to modify an array in function?

I think what you want to do is call by reference, the usage of a global variable is unnecessary.

You have a variable x, you want to give that variable to a function and you want the function to do manipulations on x directly, so that these manipulations will be visible on the xoutside your function.

This is called call by reference. Matlab doesn't support it for simple variables.

You can however use an object that inhertis from the handle - class. This will give you the desired behaviour.

If you don't want to use objects, you will have to use a return argument in your function. Don't use global variables, they will make your code unreadable and almost certainly lead to mistakes.

Community
  • 1
  • 1
Konstantin Schubert
  • 3,242
  • 1
  • 31
  • 46