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 8398157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:58:18+00:00 2026-06-09T20:58:18+00:00

I have a function that takes variadic arguments. These arguments are parameter-value pairs, so

  • 0

I have a function that takes variadic arguments. These arguments are parameter-value pairs, so varargin is a cell array in which every odd-indexed element is a string (the parameter), but the even-indexed elements can be a string, number, or cell array of strings. I want to find the index of a particular string in varargin. I have a working solution, but it uses arrayfun twice; is there a cleaner/faster/more effective way of find a string in such a cell array? The resulting index will be used to remove that element and the following one from varargin. I would like to minimize creation of new variables.

str_to_find = 'paramX'
finds = arrayfun(@(i) strfind(varargin{i},str_to_find), 1:length(varargin), 'UniformOutput', 0);
finds2 = arrayfun(@(i) ~iscell(finds{i}) && ~isempty(finds{i}), 1:length(finds));
index = find(finds2==1);

varargin(index)=[];
varargin(index)=[];

Given varargin is {'paramA', 'valueA', 'paramB', 9, 'paramX', {'z','x','c'}, then finds is [] [] [] [] [1] {1x3 cell}, finds2 is 0 0 0 0 1 0, and index is 5. So, my solution does what I need, but it just seems ugly. I would just use finds2 (i.e., not create index) to delete that element from varargin, but I also need to remove the one after it.

  • 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-09T20:58:20+00:00Added an answer on June 9, 2026 at 8:58 pm

    You can use the built-in function strcmp which should be fairly fast:

    idx  = strcmp(str_to_find, varargin);
    

    and that will give you an index to all cell elements that are strings matching the target.

    Then, for pruning those elements, you can use

    varargin( or(idx, [0 idx(1:end-1)]) ) = [];
    

    assuming that idx is a row array.

    Finally, you may also want to run some format checks to make sure that the user has not entered argument-pairs in the wrong order (or with an argument name that matches the parameter name) otherwise this kind of code will behave strangely.

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

Sidebar

Related Questions

I have a function that takes optional arguments as name/value pairs. function example(varargin) %
I have a function that takes variadic arguments, that I obtain from func_get_args() .
I have a function that takes an array as a parameter. The function then
I currently have a variadic function which takes an arbitrary number of arguments of
I have a function that takes a parameter(key) to retrieve it's value from a
I have a function that takes an array of objects and I need to
I have a function that takes two arguments, the first one $methodName being a
I have a function that takes a variable number of arguments in C that
I have a function that takes as one of it's arguments a VARRAY of
I have a function that takes either an array of IDs or a singular

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.