Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8153561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:03:35+00:00 2026-06-06T16:03:35+00:00

This is a general question, not related to a particular operation. I would like

  • 0

This is a general question, not related to a particular operation. I would like to be able to write the results of an arbitrary function into elements of a cell array without regard for the data type the function returns. Consider this pseudocode:

zout = cell(n,m);
myfunc = str2func('inputname'); %assume myfunc puts out m values to match zout dimensions
zout(1,:) = myfunc(x,y);

That will work for “inputname” == “strcat” , for example, given that x and y are strings or cells of strings with appropriate dimension. But if “inputname” == “strcmp” then the output is a logical array, and Matlab throws an error. I’d need to do

zout(1,:) = num2cell(strcmp(x,y));

So my question is: is there a way to fill the cell array zout without having to test for the type of variable generated by myfunc(x,y ? Should I be using a struct in the first place (and if so, what’s the best way to populate it)?
(I’m usually an R user, where I could just use a list variable without any pain)

Edit: To simplify the overall scope, add the following “requirement” :
Let’s assume for now that, for a function which returns multiple outputs, only the first one need be captured in zout . But when this output is a vector of N values or a vector of cells (i.e. Nx1 cell array), these N values get mapped to zout(1,1:N) .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:03:37+00:00Added an answer on June 6, 2026 at 4:03 pm

    So my question is: is there a way to fill the cell array zout without having to test for the type of variable generated by myfunc(x,y) ? Should I be using a struct in the first place (and if so, what’s the best way to populate it)?

    The answer provided by @NotBoStyf is almost there, but not quite. Cell arrays are the right way to go. However, the answer very much depends on the number of outputs from the function.

    Functions with only one output

    The function strcmp has only one output, which is an array. The reason that

    zout{1,:} = strcmp(x,y)
    

    gives you an error message, when zout is dimensioned N x 2, is that the left-hand side (zout{1,:}) expects two outputs from the right-hand side. You can fix this with:

    [zout{1,:}] = num2cell(strcmp(x,y));  % notice the square brackets on the LHS
    

    However, there’s really no reason to do this. You can simply define zout as an N x 1 cell array and capture the results:

    zout = cell(1,1);
    
    x = 'a';
    y = { 'a', 'b' };
    
    zout{1} = strcmp(x,y);
    
    % Referring to the results:
    x_is_y_1 = zout{1}(1);
    x_is_y_2 = zout{1}(2);
    

    There’s one more case to consider…

    Functions with multiple outputs

    If your function produces multiple outputs (as opposed to a single output that is an array), then this will only capture the first output. Functions that produce multiple outputs are defined like this:

    function [outA,outB] = do_something( a, b )
      outA = a + 1;
      outB = b + 2;
    end
    

    Here, you need to explicitly capture both output arguments. Otherwise, you just get a. For example:

    outA = do_something( [1,2,3], [4,5,6] ); % outA is [2,3,4]
    
    [outA,outB] = do_something( [1,2,3], [4,5,6] ); % outA is [2,3,4], outB is [6,7,8]
    
    Z1 = cell(1,1);
    Z1{1,1} = do_something( [1,2,3], [4,5,6] ); % Z1{1,1} is [2,3,4]
    
    Z2 = cell(1,2);
    Z2{1,1:2} = do_something( [1,2,3], [4,5,6] ); % Same error as above.  
    % NB: You really never want to have a cell expansion that is not surrounded 
    % by square brackets.
    
    % Do this instead:
    [Z2{1,1:2}] = do_something( [1,2,3], [4,5,6] ); % Z2{1,1} is [2,3,4], Z2{1,2} is [6,7,8]
    

    This can also be done programmatically, with some limits. Let’s say we’re given function
    func that takes one input and returns a constant (but unknown) number of outputs. We
    have cell array inp that contains the inputs we want to process, and we want to collect the results in cell around outp:

    N = numel(inp);
    M = nargout(@func);  % number of outputs produced by func 
    outp = cell(N,M);
    for i=1:N
      [ outp{i,:} ] = func( inp{i} );
    end
    

    This approach has a few caveats:

    1. It captures all of the outputs. This is not always what you want.

    2. Capturing all of the outputs can often change the behavior of the function. For example, the find function returns linear indices if only one output is used, row/column indices if two outputs are used, and row/column/value if three outputs are used.

    3. It won’t work for functions that have a variable number of outputs. These functions are defined as function [a,b,...,varargout] = func( ... ). nargout will return a negative number if the function has varargout declared in its output list, because there’s no way for Matlab to know how many outputs will be produced.

    Unpacking array and cell outputs into a cell

    All true so far, but: what I am hoping for is a generic solution. I can’t use num2cell if the function produces cell outputs. So what worked for strcmp will fail for strcat and vice versa. Let’s assume for now that, for a function which returns multiple outputs, only the first one need be captured in zout – Carl Witthoft

    To provide a uniform output syntax for all functions that return either a cell or an array, use an adapter function. Here is an example that handles numeric arrays and cells:

    function [cellOut] = cellify(input)
      if iscell(input)
        cellOut = input;
      elseif isnumeric(input)
        cellOut = num2cell(input);
      else
        error('cellify currently does not support structs or objects');
      end
    end
    

    To unpack the output into a 2-D cell array, the size of each output must be constant. Assuming M outputs:

    N = numel(inp);
    % M is known and constant
    outp = cell(N,M);
    for i=1:N
      outp(i,:) = cellify( func( inp{i} ) );  % NB: parentheses instead of curlies on LHS
    end
    

    The output can then be addressed as outp{i,j}. An alternate approach allows the size of the output to vary:

    N = numel(inp);
    % M is not necessary here
    outp = cell(N,1);
    for i=1:N
      outp{i} = cellify( func( inp{i} ) );  % NB: back to curlies on LHS
    end
    

    The output can then be addressed as outp{i}{j}, and the size of the output can vary.

    A few things to keep in mind:

    1. Matlab cells are basically inefficient pointers. The JIT compiler does not always optimize them as well as numeric arrays.

    2. Splitting numeric arrays into cells can cost quite a bit of memory. Each split value is actually a numeric array, which has size and type information associated with it. In numeric array form, this occurs once for each array. When the array is split, this incurs once for each element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a general database question, not related to any particular database or programming
I hate to ask such a general question, and this is not a write
This is a very general question that's not related to a specific language. I'm
This is a general question. And may not be specific to datagrids. How can
This question is related but not the same as this: How do you give
I would like to define a template function but disallow instantiation with a particular
This is related to this question . I'm not an expert on Linux device
I asked a related but very general question earlier (see especially this response ).
Yesterday I asked this general question about decimals and their internal precisions. Here is
This is a general question about MVC as a pattern, but in this case

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.