Initialization of persistent variable in Matlab
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
add a comment |
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
@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
add a comment |
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
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
matlab persistent
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
add a comment |
@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
add a comment |
1 Answer
1
active
oldest
votes
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.
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 ify
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 cleary
the first time you run, you should include the commandclear fcn
before the first call tofcn
.
– 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 indexingy(u>=10) = 1; y(u<=5) = 0;
.
– craigim
May 28 '14 at 3:24
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 ify
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 cleary
the first time you run, you should include the commandclear fcn
before the first call tofcn
.
– 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 indexingy(u>=10) = 1; y(u<=5) = 0;
.
– craigim
May 28 '14 at 3:24
add a comment |
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.
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 ify
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 cleary
the first time you run, you should include the commandclear fcn
before the first call tofcn
.
– 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 indexingy(u>=10) = 1; y(u<=5) = 0;
.
– craigim
May 28 '14 at 3:24
add a comment |
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.
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.
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 ify
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 cleary
the first time you run, you should include the commandclear fcn
before the first call tofcn
.
– 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 indexingy(u>=10) = 1; y(u<=5) = 0;
.
– craigim
May 28 '14 at 3:24
add a comment |
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 ify
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 cleary
the first time you run, you should include the commandclear fcn
before the first call tofcn
.
– 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 indexingy(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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@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