Thread Subject: Create structure in function LHS arguments

Subject: Create structure in function LHS arguments

From: pouncingmoose@gmail.com

Date: 17 Dec, 2008 21:42:43

Message: 1 of 2

I'm wondering if anyone can shed some light on what is happening with
the following code:

I have the following very simple function:

%%%%%%%%% Cut Here %%%%%%%%%%%%%
function [out1,out2,out3,out4] = LoadCustomData
out1 = 1; out2 = 2; out3 = 3; out4 = 4;
%%%%%%%%% Cut Here %%%%%%%%%%%%%

and I'm trying to assign the outputs from the function to a structure
in the following ways

% This works
clear all
[ad.field1{1}.outD,ad.field2.outA,ad.field2.outB] = LoadCustomData;

% This works
clear all
[ad.field2.outA,ad.field2.outB,ad.field2.outC,ad.field1{1}.outD] =
LoadCustomData;

% This does not work
clear all
[ad.field1{1}.outD,ad.field2.outA,ad.field2.outB,ad.field2.outC] =
LoadCustomData;
??? Illegal right hand side in assignment. Too many elements.

I do not understand why the first two syntaxes would work but the
third one doesn't.
The error seems to be thrown on the assignment of ad.field2.outB as
ad.field2.outA does get created.
But if that is the case then why does the first syntax work?

Please note that I'm not after suggestions related to creating the
structure inside the function and passing that out as the sole output
argument.
I'm trying to find the reason why this specific way of assigning LHS
arguments fails in some instances but not in others.

I'm using R2008b on win32.

Thanks.

Subject: Create structure in function LHS arguments

From: Walter Roberson

Date: 19 Dec, 2008 03:56:35

Message: 2 of 2

pouncingmoose@gmail.com wrote:

> I'm using R2008b on win32.

> %%%%%%%%% Cut Here %%%%%%%%%%%%%
> function [out1,out2,out3,out4] = LoadCustomData
> out1 = 1; out2 = 2; out3 = 3; out4 = 4;
> %%%%%%%%% Cut Here %%%%%%%%%%%%%

> % This does not work
> clear all
> [ad.field1{1}.outD,ad.field2.outA,ad.field2.outB,ad.field2.outC] =
> LoadCustomData;
> ??? Illegal right hand side in assignment. Too many elements.

I confirm that the same thing happens on R2008b on Linux-64.
It does not, however, happen on R2007a on Linux-64.

The work-around that I found makes no sense, but it works:

[ad.field1{1}.outD,ad.field2.outA(1),ad.field2.outB,ad.field2.outC] = LoadCustomData;

That is, adding the (1) after the outA allows the assignment to go smoothly.

Alternately,

[ad.field1{1}.outD,ad.field2(1).outA,ad.field2.outB,ad.field2.outC] = LoadCustomData;


The problem is not so easy to trigger. What I have found is:

There must be at least 4 possible output arguments to trigger the condition;

Let C stand for a cell reference, and S2 stand for a direct two-level structure
reference without any modification (e.g., X.Y.Z), and A stand for any output
argument. Then it appears that to trigger the condition, at some point in the
output arguments, there must either be C, S2, S2, A or S2, C, S2, A
S2, S2, C, A does not trigger the problem. The repetition of S2 is not intended here
to indicate that the references must duplicate each other: they can be completely
different as long as they are two-level structure references:

For example,

[P{1}, X.Y.Z, S.T.U, B] = LoadCustomData; %FAIL, 4 output arguments
[X.Y.Z, P{1}, S.T.U, B] = LoadCustomData; %FAIL, 4 output arguments
[X.Y.Z, S.T.U, P{1}, B] = LoadCustomData; %PASS, 4 output arguments

If there are enough possible output arguments, this pattern may be proceeded by
arbitrary output arguments, such as

[R, P{1}, X.Y.Z, S.T.U, B] = LoadCustomData5; %FAIL, 5 output arguments
[P{1}, X.Y.Z, S.T.U, B] = LoadCustomData6; %FAIL, 6 output arguments
[R, P{1}, X.Y.Z, S.T.U, B] = LoadCustomData6; %FAIL, 6 output arguments

However, if the function allows more possible output arguments after the
start of the described pattern, and any of those output arguments is provided,
then the problem will -not- occur:

[P{1}, X.Y.Z, S.T.U, B, R] = LoadCustomData5; %PASS, 5 output arguments
[R, P{1}, X.Y.Z, S.T.U, B, F] = LoadCustomData6; %PASS, 6 output arguments


This might not be a complete characterization of the problem, but it's enough
that I will submit a bug report.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com