Initialization of persistent variable in Matlab












0














I want to use a persistent variable inside a matlab function block but I can't initialize it. I want to either initialize it from an m.file or do it inside the function.



If I use isempty then the variable is given a size of 1x1 and I don't want that. Basically I don't know how to handle the persistent value as it is taken as 1x1 or as not defined. How can I use isempty but do not give it a 1x1 size? Or how can I initialize it from an m.file?



function y1 = fcn(u)

persistent y;

if isempty(y)
y=0;
end

for i=1:1:length(u)
if u(1,i) >=10
y(1,i) = 1;
elseif u(1,i) <= 5
y(1,i) = 0;
else
;
end
end

y1=y;

end









share|improve this question
























  • @Jigg,My persistent variable is fixed in size, let's say a vector 1x10. It changes with time since the system is dynamic. I just want it to be set to zeros only once and not every time the function is called. With isempty in the function I get 1x1 and I can't do it through workspace/m.file.
    – Engineer
    May 27 '14 at 14:43












  • @Jigg, Of course, since the variable is empty in the beginning it is a way to initialize it. It's also stated in matlab help for that reason. I will update the post with the code.
    – Engineer
    May 27 '14 at 14:49












  • As I told you it is a dynamic system, so the initialization is not important. It will run after some time properly whatever these initial conditions are.
    – Engineer
    May 27 '14 at 14:59
















0














I want to use a persistent variable inside a matlab function block but I can't initialize it. I want to either initialize it from an m.file or do it inside the function.



If I use isempty then the variable is given a size of 1x1 and I don't want that. Basically I don't know how to handle the persistent value as it is taken as 1x1 or as not defined. How can I use isempty but do not give it a 1x1 size? Or how can I initialize it from an m.file?



function y1 = fcn(u)

persistent y;

if isempty(y)
y=0;
end

for i=1:1:length(u)
if u(1,i) >=10
y(1,i) = 1;
elseif u(1,i) <= 5
y(1,i) = 0;
else
;
end
end

y1=y;

end









share|improve this question
























  • @Jigg,My persistent variable is fixed in size, let's say a vector 1x10. It changes with time since the system is dynamic. I just want it to be set to zeros only once and not every time the function is called. With isempty in the function I get 1x1 and I can't do it through workspace/m.file.
    – Engineer
    May 27 '14 at 14:43












  • @Jigg, Of course, since the variable is empty in the beginning it is a way to initialize it. It's also stated in matlab help for that reason. I will update the post with the code.
    – Engineer
    May 27 '14 at 14:49












  • As I told you it is a dynamic system, so the initialization is not important. It will run after some time properly whatever these initial conditions are.
    – Engineer
    May 27 '14 at 14:59














0












0








0







I want to use a persistent variable inside a matlab function block but I can't initialize it. I want to either initialize it from an m.file or do it inside the function.



If I use isempty then the variable is given a size of 1x1 and I don't want that. Basically I don't know how to handle the persistent value as it is taken as 1x1 or as not defined. How can I use isempty but do not give it a 1x1 size? Or how can I initialize it from an m.file?



function y1 = fcn(u)

persistent y;

if isempty(y)
y=0;
end

for i=1:1:length(u)
if u(1,i) >=10
y(1,i) = 1;
elseif u(1,i) <= 5
y(1,i) = 0;
else
;
end
end

y1=y;

end









share|improve this question















I want to use a persistent variable inside a matlab function block but I can't initialize it. I want to either initialize it from an m.file or do it inside the function.



If I use isempty then the variable is given a size of 1x1 and I don't want that. Basically I don't know how to handle the persistent value as it is taken as 1x1 or as not defined. How can I use isempty but do not give it a 1x1 size? Or how can I initialize it from an m.file?



function y1 = fcn(u)

persistent y;

if isempty(y)
y=0;
end

for i=1:1:length(u)
if u(1,i) >=10
y(1,i) = 1;
elseif u(1,i) <= 5
y(1,i) = 0;
else
;
end
end

y1=y;

end






matlab persistent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 3:16









Cœur

17.4k9102143




17.4k9102143










asked May 27 '14 at 14:35









Engineer

83




83












  • @Jigg,My persistent variable is fixed in size, let's say a vector 1x10. It changes with time since the system is dynamic. I just want it to be set to zeros only once and not every time the function is called. With isempty in the function I get 1x1 and I can't do it through workspace/m.file.
    – Engineer
    May 27 '14 at 14:43












  • @Jigg, Of course, since the variable is empty in the beginning it is a way to initialize it. It's also stated in matlab help for that reason. I will update the post with the code.
    – Engineer
    May 27 '14 at 14:49












  • As I told you it is a dynamic system, so the initialization is not important. It will run after some time properly whatever these initial conditions are.
    – Engineer
    May 27 '14 at 14:59


















  • @Jigg,My persistent variable is fixed in size, let's say a vector 1x10. It changes with time since the system is dynamic. I just want it to be set to zeros only once and not every time the function is called. With isempty in the function I get 1x1 and I can't do it through workspace/m.file.
    – Engineer
    May 27 '14 at 14:43












  • @Jigg, Of course, since the variable is empty in the beginning it is a way to initialize it. It's also stated in matlab help for that reason. I will update the post with the code.
    – Engineer
    May 27 '14 at 14:49












  • As I told you it is a dynamic system, so the initialization is not important. It will run after some time properly whatever these initial conditions are.
    – Engineer
    May 27 '14 at 14:59
















@Jigg,My persistent variable is fixed in size, let's say a vector 1x10. It changes with time since the system is dynamic. I just want it to be set to zeros only once and not every time the function is called. With isempty in the function I get 1x1 and I can't do it through workspace/m.file.
– Engineer
May 27 '14 at 14:43






@Jigg,My persistent variable is fixed in size, let's say a vector 1x10. It changes with time since the system is dynamic. I just want it to be set to zeros only once and not every time the function is called. With isempty in the function I get 1x1 and I can't do it through workspace/m.file.
– Engineer
May 27 '14 at 14:43














@Jigg, Of course, since the variable is empty in the beginning it is a way to initialize it. It's also stated in matlab help for that reason. I will update the post with the code.
– Engineer
May 27 '14 at 14:49






@Jigg, Of course, since the variable is empty in the beginning it is a way to initialize it. It's also stated in matlab help for that reason. I will update the post with the code.
– Engineer
May 27 '14 at 14:49














As I told you it is a dynamic system, so the initialization is not important. It will run after some time properly whatever these initial conditions are.
– Engineer
May 27 '14 at 14:59




As I told you it is a dynamic system, so the initialization is not important. It will run after some time properly whatever these initial conditions are.
– Engineer
May 27 '14 at 14:59












1 Answer
1






active

oldest

votes


















1














You are initializing y to a scalar. If you want to initialize it to an empty vector of zeros, use y=zeros(1,n) where n is the number of elements you want it to have.






share|improve this answer





















  • If I do that IN the function, y will be set to 0 every time the function is called. But I want to keep the values, that's why I have a persistent variable.
    – Engineer
    May 27 '14 at 15:13








  • 1




    Only if y is empty. isempty returns either true or false. If true, then it initializes to an empty vector. if false, it skips that line and uses the persistent values that are already stored in the variable.
    – craigim
    May 27 '14 at 15:15










  • One other thing to pay attention to. The values are persistent as long as MATLAB is running. In order to clear y the first time you run, you should include the command clear fcn before the first call to fcn.
    – craigim
    May 27 '14 at 15:19










  • Yes, it finally worked! I thought I had to run through the elements but a check of just y is enough. That's why I couldn't run it, because I was trying to check individual cells. In my case doing it for the whole vector is the same. Thanks a lot!
    – Engineer
    May 27 '14 at 15:23










  • Here is some bonus help. The for/end block can be vectorized and completed in just two steps using logical indexing y(u>=10) = 1; y(u<=5) = 0;.
    – craigim
    May 28 '14 at 3:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23892262%2finitialization-of-persistent-variable-in-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You are initializing y to a scalar. If you want to initialize it to an empty vector of zeros, use y=zeros(1,n) where n is the number of elements you want it to have.






share|improve this answer





















  • If I do that IN the function, y will be set to 0 every time the function is called. But I want to keep the values, that's why I have a persistent variable.
    – Engineer
    May 27 '14 at 15:13








  • 1




    Only if y is empty. isempty returns either true or false. If true, then it initializes to an empty vector. if false, it skips that line and uses the persistent values that are already stored in the variable.
    – craigim
    May 27 '14 at 15:15










  • One other thing to pay attention to. The values are persistent as long as MATLAB is running. In order to clear y the first time you run, you should include the command clear fcn before the first call to fcn.
    – craigim
    May 27 '14 at 15:19










  • Yes, it finally worked! I thought I had to run through the elements but a check of just y is enough. That's why I couldn't run it, because I was trying to check individual cells. In my case doing it for the whole vector is the same. Thanks a lot!
    – Engineer
    May 27 '14 at 15:23










  • Here is some bonus help. The for/end block can be vectorized and completed in just two steps using logical indexing y(u>=10) = 1; y(u<=5) = 0;.
    – craigim
    May 28 '14 at 3:24
















1














You are initializing y to a scalar. If you want to initialize it to an empty vector of zeros, use y=zeros(1,n) where n is the number of elements you want it to have.






share|improve this answer





















  • If I do that IN the function, y will be set to 0 every time the function is called. But I want to keep the values, that's why I have a persistent variable.
    – Engineer
    May 27 '14 at 15:13








  • 1




    Only if y is empty. isempty returns either true or false. If true, then it initializes to an empty vector. if false, it skips that line and uses the persistent values that are already stored in the variable.
    – craigim
    May 27 '14 at 15:15










  • One other thing to pay attention to. The values are persistent as long as MATLAB is running. In order to clear y the first time you run, you should include the command clear fcn before the first call to fcn.
    – craigim
    May 27 '14 at 15:19










  • Yes, it finally worked! I thought I had to run through the elements but a check of just y is enough. That's why I couldn't run it, because I was trying to check individual cells. In my case doing it for the whole vector is the same. Thanks a lot!
    – Engineer
    May 27 '14 at 15:23










  • Here is some bonus help. The for/end block can be vectorized and completed in just two steps using logical indexing y(u>=10) = 1; y(u<=5) = 0;.
    – craigim
    May 28 '14 at 3:24














1












1








1






You are initializing y to a scalar. If you want to initialize it to an empty vector of zeros, use y=zeros(1,n) where n is the number of elements you want it to have.






share|improve this answer












You are initializing y to a scalar. If you want to initialize it to an empty vector of zeros, use y=zeros(1,n) where n is the number of elements you want it to have.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 27 '14 at 15:07









craigim

3,22711435




3,22711435












  • If I do that IN the function, y will be set to 0 every time the function is called. But I want to keep the values, that's why I have a persistent variable.
    – Engineer
    May 27 '14 at 15:13








  • 1




    Only if y is empty. isempty returns either true or false. If true, then it initializes to an empty vector. if false, it skips that line and uses the persistent values that are already stored in the variable.
    – craigim
    May 27 '14 at 15:15










  • One other thing to pay attention to. The values are persistent as long as MATLAB is running. In order to clear y the first time you run, you should include the command clear fcn before the first call to fcn.
    – craigim
    May 27 '14 at 15:19










  • Yes, it finally worked! I thought I had to run through the elements but a check of just y is enough. That's why I couldn't run it, because I was trying to check individual cells. In my case doing it for the whole vector is the same. Thanks a lot!
    – Engineer
    May 27 '14 at 15:23










  • Here is some bonus help. The for/end block can be vectorized and completed in just two steps using logical indexing y(u>=10) = 1; y(u<=5) = 0;.
    – craigim
    May 28 '14 at 3:24


















  • If I do that IN the function, y will be set to 0 every time the function is called. But I want to keep the values, that's why I have a persistent variable.
    – Engineer
    May 27 '14 at 15:13








  • 1




    Only if y is empty. isempty returns either true or false. If true, then it initializes to an empty vector. if false, it skips that line and uses the persistent values that are already stored in the variable.
    – craigim
    May 27 '14 at 15:15










  • One other thing to pay attention to. The values are persistent as long as MATLAB is running. In order to clear y the first time you run, you should include the command clear fcn before the first call to fcn.
    – craigim
    May 27 '14 at 15:19










  • Yes, it finally worked! I thought I had to run through the elements but a check of just y is enough. That's why I couldn't run it, because I was trying to check individual cells. In my case doing it for the whole vector is the same. Thanks a lot!
    – Engineer
    May 27 '14 at 15:23










  • Here is some bonus help. The for/end block can be vectorized and completed in just two steps using logical indexing y(u>=10) = 1; y(u<=5) = 0;.
    – craigim
    May 28 '14 at 3:24
















If I do that IN the function, y will be set to 0 every time the function is called. But I want to keep the values, that's why I have a persistent variable.
– Engineer
May 27 '14 at 15:13






If I do that IN the function, y will be set to 0 every time the function is called. But I want to keep the values, that's why I have a persistent variable.
– Engineer
May 27 '14 at 15:13






1




1




Only if y is empty. isempty returns either true or false. If true, then it initializes to an empty vector. if false, it skips that line and uses the persistent values that are already stored in the variable.
– craigim
May 27 '14 at 15:15




Only if y is empty. isempty returns either true or false. If true, then it initializes to an empty vector. if false, it skips that line and uses the persistent values that are already stored in the variable.
– craigim
May 27 '14 at 15:15












One other thing to pay attention to. The values are persistent as long as MATLAB is running. In order to clear y the first time you run, you should include the command clear fcn before the first call to fcn.
– craigim
May 27 '14 at 15:19




One other thing to pay attention to. The values are persistent as long as MATLAB is running. In order to clear y the first time you run, you should include the command clear fcn before the first call to fcn.
– craigim
May 27 '14 at 15:19












Yes, it finally worked! I thought I had to run through the elements but a check of just y is enough. That's why I couldn't run it, because I was trying to check individual cells. In my case doing it for the whole vector is the same. Thanks a lot!
– Engineer
May 27 '14 at 15:23




Yes, it finally worked! I thought I had to run through the elements but a check of just y is enough. That's why I couldn't run it, because I was trying to check individual cells. In my case doing it for the whole vector is the same. Thanks a lot!
– Engineer
May 27 '14 at 15:23












Here is some bonus help. The for/end block can be vectorized and completed in just two steps using logical indexing y(u>=10) = 1; y(u<=5) = 0;.
– craigim
May 28 '14 at 3:24




Here is some bonus help. The for/end block can be vectorized and completed in just two steps using logical indexing y(u>=10) = 1; y(u<=5) = 0;.
– craigim
May 28 '14 at 3:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23892262%2finitialization-of-persistent-variable-in-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'