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:
Size-function and ";"

Subject: Size-function and ";"

From: Adam Andersen L?ss?e

Date: 27 Oct, 2009 13:08:20

Message: 1 of 12

I have this very annoying problem, that I have now encoutered a few times:

Excerpt of the code:
------
...
for i=1:nelb
    sys.T{i+nelb}= [i+1 i+nelb+1 i+nelb+2] % ";" gives error?!
end

size(sys.T,2)
------
(some other stuff is before this, but this is what seems to be causing the problems.)

If the operation in the for-loop is ended with a ";" then "size(sys.T,2)" returns a value, as if the loop has not been run at all. If ";" is omitted it returns fine, but I of course have a very cluttered command window.

If I run the program with the ";" and then after the program has finished type "size(sys.T,2)" in the command window, it returns the proper result.

What is happening and how do I fix it?

Subject: Size-function and ";"

From: dpb

Date: 27 Oct, 2009 13:20:37

Message: 2 of 12

Adam Andersen L?ss?e wrote:
> I have this very annoying problem, that I have now encoutered a few times:
>
> Excerpt of the code:
> ------
> ...
> for i=1:nelb
> sys.T{i+nelb}= [i+1 i+nelb+1 i+nelb+2] % ";" gives error?!
> end
>
> size(sys.T,2)
> ------
> (some other stuff is before this, but this is what seems to be causing the problems.)
>
> If the operation in the for-loop is ended with a ";" then
> "size(sys.T,2)" returns a value, as if the loop has not been run at
> all. If ";" is omitted it returns fine, but I of course have a very
> cluttered command window.
>
> If I run the program with the ";" and then after the program has
> finished type "size(sys.T,2)" in the command window, it returns the
> proper result.
>
> What is happening and how do I fix it?

Think you'll need to post a full example that demonstrates the problem
w/o excerpting and trying to interpret the error messages. You've
"concised" it down too far...

--

Subject: Size-function and ";"

From: Adam Andersen L?ss?e

Date: 27 Oct, 2009 17:42:19

Message: 3 of 12

Okay, here is the entire code up until the problem:

% DESCRIPTION ---------------------------------------------------
% 2D heat flow applying plane triangular elements of the lpt type
% with linear field approximation.

close all, clear all

% Independent parameters
a=8e-3;
b=12*a;
nelb=4;
El=b/2*nelb;
% lx=2.0; % length of the domain
% ly=1.0; % width of the domain

% TOPOLOGY ------------------------------------------------------
% node coord's

for i=1:2*nelb+1
    sys.X(i,:)=[(i-1)*El 0.0];
end

for i=1:2*nelb+1
    sys.X=[sys.X ; [(i-1)*El 0.5]];
end

for i=1:2*nelb+1
    sys.X=[sys.X ; [(i-1)*El 1]];
end

%el topo
nno=size(sys.X,1);
counter=0;
for i=1:2:2*nelb
    counter=counter+1;
    sys.T{counter}=[i i+2 nno-2*nelb+(i-1)];
end
for i=1:2:2*nelb %This entire loop seems to be disregarded by the following size-func.
    counter=counter+1;
    sys.T{counter}=[i+2 nno-2*nelb+(i+1) nno-2*nelb+(i-1)] % ";"-problem!
end

size(sys.T,2)
----------------------------------------

If ";" is omitted from 4th line from the bottom size(sys.T,2) works as expected, and returns 8.
If ";" is included size(sys.T,2) returns only 4. This corresponds to the last loop being omitted. When, however I type size(sys.T,2) in the command window after the code has been executed, it returns the proper result, 8.

Subject: Size-function and ";"

From: dpb

Date: 27 Oct, 2009 18:12:32

Message: 4 of 12

Adam Andersen L?ss?e wrote:
> Okay, here is the entire code up until the problem:

...[code elided for brevity]...

> If ";" is omitted from 4th line from the bottom size(sys.T,2) works
> as expected, and returns 8.
> If ";" is included size(sys.T,2) returns only 4. This corresponds to
> the last loop being omitted. When, however I type size(sys.T,2) in
> the command window after the code has been executed, it returns the
> proper result, 8.

I pasted your code into a file tryit.m and ran it w/ and w/o the subject ";"

 >> tryit
sys =
     X: [27x2 double]
     T: {[1 3 19] [3 5 21] [5 7 23] [7 9 25] [3 21 19]}
sys =
     X: [27x2 double]
     T: {[1 3 19] [3 5 21] [5 7 23] [7 9 25] [3 21 19] [5 23 21]}
sys =
     X: [27x2 double]
     T: {[1 3 19] [3 5 21] [5 7 23] [7 9 25] [3 21 19] [5 23 21]
[7 25 23]}
sys =
     X: [27x2 double]
     T: {1x8 cell}
ans =
      8
 >> tryit
ans =
      8
 >> sys
sys =
     X: [27x2 double]
     T: {1x8 cell}
 >>

No discernible difference here...

(R12, rather old; perhaps somebody w/ newer version can check as well)

--

Subject: Size-function and ";"

From: Matt Fig

Date: 27 Oct, 2009 19:32:03

Message: 5 of 12

I can confirm this in 2007b.

If the semicolon is added, but a simple

pause(.001)

is placed before the call to size, the problem disappears. When the call to pause is removed, the incorrect size is returned. Why should this depend on a flushing of the graphic events queue? Something has gone afoul. Here is the code I used, compacted a little bit:


close all, clear all
a=8e-3; b=12*a; nelb=4; El=b/2*nelb;
for i=1:2*nelb+1
    sys.X(i,:)=[(i-1)*El 0.0];
end
for i=1:2*nelb+1
    sys.X=[sys.X ; [(i-1)*El 0.5]];
end
for i=1:2*nelb+1
    sys.X=[sys.X ; [(i-1)*El 1]];
end
nno=size(sys.X,1);
counter=0;
for i=1:2:2*nelb
    counter=counter+1;
    sys.T{counter}=[i i+2 nno-2*nelb+(i-1)];
end
for ii=1:2:2*nelb %Thiis entire loop seems to be disregarded by the following size-func.
    counter=counter+1;
    sys.T{counter} = [ii+2 nno-2*nelb+(ii+1) nno-2*nelb+(ii-1)]; % ";"-problem!
end
pause(.001) % comment this out to get a different value.
TT = size(sys.T,2)



Also, a whos statement can be used instead of a pause, and putting the code in a function instead of a script has no effect.

Subject: Size-function and ";"

From: dpb

Date: 27 Oct, 2009 20:55:24

Message: 6 of 12

Matt Fig wrote:
> I can confirm this in 2007b.
>
> If the semicolon is added, but a simple
>
> pause(.001)
>
> is placed before the call to size, the problem disappears. When the
> call to pause is removed, the incorrect size is returned. ...

This looks like OP should submit a bug report to TMW, then...

--

Subject: Size-function and ";"

From: Jan Simon

Date: 27 Oct, 2009 21:07:05

Message: 7 of 12

Dear Adam!

I confirm this for Matlab 2009a.
Matlab R13 (6.5.1) works well.

I've reduced the problem again:
 
  S = [];
  counter = 0;
  for i = 1:2
     counter = counter + 1;
     S.T{counter} = 1;
  end
  for i = 1:2
     counter = counter + 1;
     S.T{counter} = 2;
  end
  % pause(.001) % comment this out to get a different SIZE
  % S.T{end} = 5; % does not help
  lengthT = length(S.T);
  sizeT = size(S.T);
  disp(sizeT);
  disp(lengthT);
  % X = S.T; sizeX = size(X); % correct!

Reply:
  1 2 % This is the SIZE
  4 % This is the LENGTH, same for NUMEL

1. To my deep surprise only the SIZE is concerned, but the LENGTH is correct. NUMEL works fine, too.
2. The order of the SIZE and LENGTH commands does not matter.
3. "disp(size(S.T))" replies the correct size!
4. Further loops can follow, but SIZE replies the same dimensions as after the first loop.
5. This bug appears for structs only. If [T] is used directly, the replies of SIZE and LENGTH are correct.
6. If [S.T] is not a cell but a vector, the problem does not appear.
7. As you can imagine, disabling the just-in-time acceleration let vanish the problem: feature('accel', 'off').
8. Using a different counter for the 2nd loop does not change the problem, e.g. counter2. Same for greater steps, e.g. counter = counter + 2;
9. Expanding [S.T] in another direction in the 2nd loop does change the problem, e.g. "S.T{counter, 2} = 2;"
10. "disp(S.T)" works well.
11. "S.T{end} = 5" inserts the 5 in the correct last position, but the reply of SIZE remains invalid.
12. A read access to [S.T] fixes the problem.

Please TWM, fix this JIT bug for as many old Matlab versions as possible.

Thanks Adam for finding this bug! Jan

Subject: Size-function and ";"

From: Jan Simon

Date: 28 Oct, 2009 09:02:03

Message: 8 of 12

Dear Adam!

Did you send a bug report to The MathWorks?

Thanks, Jan

Subject: Size-function and ";"

From: Adam Andersen L?ss?e

Date: 28 Oct, 2009 13:43:04

Message: 9 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hc91ab$a9c$1@fred.mathworks.com>...
> Dear Adam!
>
> Did you send a bug report to The MathWorks?
>
> Thanks, Jan

I didn't see all the replies until now. I can't seem to get the notification feature work.
No, I haven't filed a bug report. How do I do that? Maybe you, who evidently ran some more extensive bug-analysis would find it easy?

Thanks for all the quick replies!

I

Subject: Size-function and ";"

From: Jan Simon

Date: 28 Oct, 2009 17:13:02

Message: 10 of 12

Dear Adam!

> No, I haven't filed a bug report. How do I do that? Maybe you, who evidently ran some more extensive bug-analysis would find it easy?

I filled out the bug report and mentioned, that you have discovered this bug.

Jan

Subject: Size-function and ";"

From: Adam Andersen L?ss?e

Date: 28 Oct, 2009 18:22:03

Message: 11 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hc91ab$a9c$1@fred.mathworks.com>...
> Dear Adam!
>
> Did you send a bug report to The MathWorks?
>
> Thanks, Jan

I didn't see all the replies until now. I can't seem to get the notification feature work.
No, I haven't filed a bug report. How do I do that? Maybe you, who evidently ran some more extensive bug-analysis would find it easy?

Thanks for all the quick replies!

I

Subject: Size-function and ";"

From: Martin Demel

Date: 29 Oct, 2009 11:59:02

Message: 12 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hc9u2u$r25$1@fred.mathworks.com>...
> Dear Adam!
>
> > No, I haven't filed a bug report. How do I do that? Maybe you, who evidently ran some more extensive bug-analysis would find it easy?
>
> I filled out the bug report and mentioned, that you have discovered this bug.
>
> Jan


Thanks, Jan, for reporting this bug to The Mathworks Technical Support! It's about the way the JIT/Accelerator treats code with structure based indices.
I verified this behaviour in R2009a and earlier but could not reproduce this issue in R2009b. So an upgrade would help in this case here. For previous versions you have to use one of the following workarounds:

To workaround this issue, use any of the following options:
feature jit off
feature accel off
or,
feature jitallow structs off

Our development is aware of the indexing issue in combination with the accelerator and resolved most of related issues, but there may be still some left so please report them if you find some others.

Martin
Technical Support, The MathWorks

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