0

I am not sure why i can't do this. Why cant I declare a variable outside a function before it gets used. I get error an error saying temp is an undefined function or variable. I realize I can pass the variable into the function thisblah(temp) but that is not what I want to do. The below is a shortened/redacted version of what I want to do. I am trying to add functionality in an existing function and want to know how many times I enter. Also I know you can solve calling the temp variable persistent inside the function but I dont think that is right answer. Global doesnt work either.

temp = 0;

for i = 1:5
           thisblah
end


function thisblah
  temp = temp + 1;
  if temp(3)
    fprintf('yes a three');
  end
  fprintf('temp is %d, temp);
end
3
  • 5
    Functions have their own variable scope, please read this: blogs.mathworks.com/loren/2008/01/16/… You are best off adding an input and output to your function to properly control this. An alternative would be to make temp either persistent or global (which should work, you've given no details why it doesn't for you), but I think you should understand the basics around scoping first Commented Dec 18, 2020 at 8:36
  • 4
    "but that is not what I want to do" In most cases that is what you want to do, so if your case is so special that it requires another aproach you should explain why (what in your problem is excluding the simplest/more robust approach). Commented Dec 18, 2020 at 10:25
  • The reason I had trouble with this concept is because of it's use within an .m file. At least the way I use .m files it seems variables should be held in that .m file scope. For example with .m file use there is no need for a "main" function (I think?) so when I call functions they float within the m file. Accepting answer, thanks. Commented Jan 3, 2021 at 21:09

1 Answer 1

1

Global Approach:

As the comments above have suggested an example of a global approach (making temp a global variable) is below. Also, temp will not have an 3rd index, temp(3) by simply incrementing it. To check when the function has been entered 3 times you can check if temp == 3.

global temp;
temp = 0;

for i = 1: 5
    thisblah
end

function thisblah

global temp;
  temp = temp + 1;
  if temp == 3
    fprintf('yes a three\n');
  end
  fprintf('temp is %d\n', temp);

end

Persistent Approach:

If you make the function in a separate .m file you can simply call clear thisblah instead of clear functions which needs to be there to clear the persistent variable after the script is done being run or before the script is re-run.

clc;
for i = 1: 5
    thisblah
end

clear functions;

function thisblah
persistent temp

if isempty(temp)
        temp = 0;
end

temp = temp + 1;
  
  if temp == 3
    fprintf('yes a three\n');
  end
  fprintf('temp is %d\n', temp);

end

Also, the syntax of line needs changing from:

fprintf('temp is %d, temp);

to

fprintf('temp is %d\n', temp);

Ran using MATLAB R2019b

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.