Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
editor preferences

Subject: editor preferences

From: Yuri Geshelin

Date: 2 Feb, 2012 15:28:12

Message: 1 of 6

Just curious:

When statement

a(i) = 0;

is inside a loop, the editor warns you:

“The variable ‘a’ appears to change size on every loop iteration. Consider preallocating for speed.”

This is understandable. But once I include

persistent a

in the code, the warning is gone.

I experimented with big arrays, tested the code with and without PERSISTENT and found that in both cases preallocation makes a code equally faster. So why should the presence of PERSISTENT disable the warning, what is the logic behind this?

Subject: editor preferences

From: ImageAnalyst

Date: 2 Feb, 2012 16:02:43

Message: 2 of 6

That's not preallocating. That's just making sure the variable hangs
around for the next call to that function instead of vanishing once
the function exits. You need to figure out how many times your loop
runs and then say something like

a = zeros(1234); % Loop should run 1234
for k = 1 : 1234
  a(k) = k * cos(k) * 42; % Whatever...
end

You can run more than 1234 times, but at least you'll get those 1234
elements preallocated rather than having to allocate one more element
each time.

P.S. Don't use i (the complex variable) as a variable. They warn
against that.

Subject: editor preferences

From: Yuri Geshelin

Date: 2 Feb, 2012 16:51:09

Message: 3 of 6

Thanks, but you must have misunderstood my question. I know what preallocation is, and I know what PERSISTENT does.

Here is the question, once again.

Why does the editor's warning disappear once you include PERSISTENT in the code?

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <66572c8b-af60-4e78-b639-4a89ccdd248f@ow3g2000pbc.googlegroups.com>...
> That's not preallocating. That's just making sure the variable hangs
> around for the next call to that function instead of vanishing once
> the function exits. You need to figure out how many times your loop
> runs and then say something like
>
> a = zeros(1234); % Loop should run 1234
> for k = 1 : 1234
> a(k) = k * cos(k) * 42; % Whatever...
> end
>
> You can run more than 1234 times, but at least you'll get those 1234
> elements preallocated rather than having to allocate one more element
> each time.
>
> P.S. Don't use i (the complex variable) as a variable. They warn
> against that.

Subject: editor preferences

From: ImageAnalyst

Date: 2 Feb, 2012 17:56:09

Message: 4 of 6

Sorry - I don't know.

Subject: editor preferences

From: Steven_Lord

Date: 2 Feb, 2012 18:19:44

Message: 5 of 6



"Yuri Geshelin" <geshelin@hotmail.com> wrote in message
news:jgeett$b7a$1@newscl01ah.mathworks.com...
> Thanks, but you must have misunderstood my question. I know what
> preallocation is, and I know what PERSISTENT does.
>
> Here is the question, once again.
>
> Why does the editor's warning disappear once you include PERSISTENT in the
> code?

Because Code Analyzer can't be sure that you didn't preallocate the variable
in a previous call to the function. When the variable was not persistent,
barring things like EVAL, LOAD, ASSIGNIN, etc. it knew what "happened" to
variables just by looking at the code as it is now. With PERSISTENT, it
would need to deduce what could have happened to the variable on every code
path the function could have taken on a previous run, and that could be
difficult or even impossible to do without running the code, which Code
Analyzer does not do.

In general, if Code Analyzer shows you a red message you do have a bug in
your code. You have too many or too few parentheses, you're missing an END,
you've tried to name a variable 12345Info [variable names must start with a
letter], or something like that. Your syntax is wrong, and your code will
NOT execute. If Code Analyzer shows you an orange message, it's found
something that could be a problem or could be improved and it suggests you
review that piece of the code to determine if it is a problem. It may not
be. But it's bringing it to your attention.

> ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> <66572c8b-af60-4e78-b639-4a89ccdd248f@ow3g2000pbc.googlegroups.com>...
>> That's not preallocating. That's just making sure the variable hangs
>> around for the next call to that function instead of vanishing once
>> the function exits. You need to figure out how many times your loop
>> runs and then say something like
>>
>> a = zeros(1234); % Loop should run 1234

*cough* zeros(1, 1234);

That's something I'd love to have Code Analyzer catch and warn about, but
I'm not sure it could do so reliably without lots of false positives. Still,
maybe I'll enter that into the enhancement database or check that it's
already there.

>> for k = 1 : 1234
>> a(k) = k * cos(k) * 42; % Whatever...
>> end
>>
>> You can run more than 1234 times, but at least you'll get those 1234
>> elements preallocated rather than having to allocate one more element
>> each time.
>>
>> P.S. Don't use i (the complex variable) as a variable. They warn
>> against that.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: editor preferences

From: Yuri Geshelin

Date: 2 Feb, 2012 19:36:10

Message: 6 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <jgek40$sci$1@newscl01ah.mathworks.com>...

> With PERSISTENT, it
> would need to deduce what could have happened to the variable on every code
> path the function could have taken on a previous run, and that could be
> difficult or even impossible to do without running the code, which Code
> Analyzer does not do.

You must be referring to the situation when that variable is defined and used as a class, right?

I can't think of any other case when a variable can be changed other than in the code itself.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us