Thread Subject: Passing Cell Array Data to a Structure...

Subject: Passing Cell Array Data to a Structure...

From: Andrey Kazak

Date: 22 Sep, 2009 19:37:01

Message: 1 of 3

Greetings!

I have a function F that returns a cell array with dimension 1x2. I want to call the function F and at the same time to pass output data to specific fields of early created stucture S, e. g. something like this:

[S.a S.b] = F(...)

Is there any way to do such assignment in MATLAB?

Subject: Passing Cell Array Data to a Structure...

From: Jan Simon

Date: 23 Sep, 2009 08:29:03

Message: 2 of 3

Dear Andrey Kazak!

> I have a function F that returns a cell array with dimension 1x2. I want to call the function F and at the same time to pass output data to specific fields of early created stucture S, e. g. something like this:
>
> [S.a S.b] = F(...)
>
> Is there any way to do such assignment in MATLAB?

Yes, it works.
[A, B] = function F
  C = {'a', 'b'}; % Just as example!
  A = C{1};
  B = C{2};
return;

Now you can call F and write the reply in the fields of a struct:
  [S.a, S.b] = F
The field can, but need not exist before calling.
In Matlab 5.3 (perhaps 6.0, 6.1), the fields must exist before.

Good luck, Jan

Subject: Passing Cell Array Data to a Structure...

From: Jos

Date: 23 Sep, 2009 12:30:40

Message: 3 of 3

"Andrey Kazak" <AK@nospam.ru> wrote in message <h9b90t$21c$1@fred.mathworks.com>...
> Greetings!
>
> I have a function F that returns a cell array with dimension 1x2. I want to call the function F and at the same time to pass output data to specific fields of early created stucture S, e. g. something like this:
>
> [S.a S.b] = F(...)
>
> Is there any way to do such assignment in MATLAB?

See CELL2STRUCT and/or DEAL

% your function
F = @() {'hello',2}
% call the function and put them into a (new structure)
S = cell2struct(F(),{'a','b'},2)
% or
C = F()
[S2.a, S2.b] = deal(C{:})

hth
Jos

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
cell array stru... Andrey Kazak 22 Sep, 2009 15:39:04
rssFeed for this Thread

Contact us at files@mathworks.com