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:
Do I really need eval() for this??

Subject: Do I really need eval() for this??

From: Thomas Clark

Date: 16 Nov, 2008 14:14:02

Message: 1 of 6

Hi all,

I have vectors of data in a cell array, and use them as input to ndgrid:

mydata = {[1 2 3] ; [2 3 4 5]; [2 2 2 3]};
[X1 X2 X3] = ndgrid(mydata{1}, mydata{2}, mydata{3})

This is fine, but I'd like to generalise the statement so that there can be any number of cell elements in mydata - in this case, of course, there are three.

I only need up to 9 - so could put in a long switch statement, but that seems crass. Alternatively, I could use eval... but I'd prefer not to for about a billion reasons!

Any other ideas?

Kind regards, and thanks

Tom Clark

Subject: Do I really need eval() for this??

From: Husam Aldahiyat

Date: 16 Nov, 2008 16:19:02

Message: 2 of 6

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <gfp9ra$3f3$1@fred.mathworks.com>...
> Hi all,
>
> I have vectors of data in a cell array, and use them as input to ndgrid:
>
> mydata = {[1 2 3] ; [2 3 4 5]; [2 2 2 3]};
> [X1 X2 X3] = ndgrid(mydata{1}, mydata{2}, mydata{3})
>
> This is fine, but I'd like to generalise the statement so that there can be any number of cell elements in mydata - in this case, of course, there are three.
>
> I only need up to 9 - so could put in a long switch statement, but that seems crass. Alternatively, I could use eval... but I'd prefer not to for about a billion reasons!
>
> Any other ideas?
>
> Kind regards, and thanks
>
> Tom Clark

Ummm, use:

[X1 X2 X3] = ndgrid(mydata{1:length(mydata)})

Does this help?

Subject: Do I really need eval() for this??

From: Thomas Clark

Date: 16 Nov, 2008 17:55:05

Message: 3 of 6

Husam,

Getting there... but I'd still have to have some way of assigning the output arrays directly into a cell. The syntax

xout = cell(3,1)
xout = ndgrid(mydata{1:length(mydata)})

and
xout = cell(3,1)
xout{:} = ndgrid(mydata{1:length(mydata)})

are both incorrect / produce errors.

Thanks, though!

Tom C

Subject: Do I really need eval() for this??

From: matt dash

Date: 16 Nov, 2008 17:59:03

Message: 4 of 6

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <gfp9ra$3f3$1@fred.mathworks.com>...
> Hi all,
>
> I have vectors of data in a cell array, and use them as input to ndgrid:
>
> mydata = {[1 2 3] ; [2 3 4 5]; [2 2 2 3]};
> [X1 X2 X3] = ndgrid(mydata{1}, mydata{2}, mydata{3})
>
> This is fine, but I'd like to generalise the statement so that there can be any number of cell elements in mydata - in this case, of course, there are three.
>
> I only need up to 9 - so could put in a long switch statement, but that seems crass. Alternatively, I could use eval... but I'd prefer not to for about a billion reasons!
>
> Any other ideas?
>
> Kind regards, and thanks
>
> Tom Clark


I'd recommend storing the results of ndgrid in a cell array as well:

mydata={[5 7 8];[4 2 6 7];[4 9 8 5 3];[4 6 5]};
X=cell(numel(mydata),1);
[X{:}]=ndgrid(mydata{:});

Subject: Do I really need eval() for this??

From: Husam Aldahiyat

Date: 16 Nov, 2008 19:33:02

Message: 5 of 6

Whoops, didn't see that.

Subject: Do I really need eval() for this??

From: Rodney Thomson

Date: 17 Nov, 2008 00:27:01

Message: 6 of 6

Well, you need 1 output for each input, you could do something similar to below:

% doubleMe - Doubles each input argument and returns it
function varargout = doubleMe(varargin)

    % MUST have the same number of outputs as intputs
    assert(nargout == nargin);
    
    for i_input = 1:nargin
        varargout{i_input} = 2*varargin{i_input};
    end
end

Cheers

Rod

--
http://iheartmatlab.blogspot.com

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