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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:46:33+00:00 2026-05-26T23:46:33+00:00

I do scientific image processing in MATLAB, which requires solving weird optimization problems with

  • 0

I do scientific image processing in MATLAB, which requires solving weird optimization problems with many parameters that are used by an “experiment” function which has many different “helper functions” that use various subsets of the parameters. Passing these parameters around is a pain and I would like an elegant extensible solution.

My preferred solution would be a “relative global” variable – common to a “main” function’s workspace, as well as any subfunctions that the main function calls and that specify they want to share that variable. But the relative global variable would not exist outside the function which declares it, and would disappear once the main function returns.

The code would look like this, except there would be many more experiment_helpers, each using the parameters in different ways:

function y = experiment(p1,p2,p3,...)

% declare relative global variables.
% these will not change value within this experiment function,
% but this experiment will be reused several times in the calling function,
% each time with different parameter values.
relative global p1, p2, p3 ...

% load and process some data based on parameters
...

% call the helper + return
y = experiment_helper(y,z);

end

function y = experiment_helper(y,z)

relative global p1, p3

%% do some stuff with y, z, possibly using p1 and p3, but not changing either of them.
...

end

I realize that you can get the desired behavior in several other ways — you could pass the parameters to the sub-functions called by the experiment, you could put the parameters in a parameter structure and pass them to the sub-functions, and so on. The first is horrible because I have to change all the subfunctions’ arguments every time I want to change the use of parameters. The second is okay, but I have to include the structure prefix every time I want to use the variables.

I suppose the “proper” solution to my problem is to use options structures much like matlab does in its own optimization code. I just wonder if there isn’t a slicker way that doesn’t involve me typing “paramStruct.value” every time I want to use the parameter “value”.

I realize that relative global variables could cause all sorts of debugging nightmares, but I don’t think they would necessarily be worse than the ones caused by existing global variables. And they would at least have more locality than unqualified globals.

So how does a real programmer handle this problem? Is there an elegant, easy to create and maintain design for this kind of problem? Can it be done in matlab? If not, how would you do it in another language? (I always feel guilty about using matlab as it doesn’t exactly encourage good design. I always want to switch to python but can’t justify relearning things and migrating the code base — unless it would make me a much faster and better programmer within a few weeks, and the matlab-specific tools, such as wavelet and optimization toolboxes, could quickly+easily be found or duplicated within python.)

  • 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-05-26T23:46:33+00:00Added an answer on May 26, 2026 at 11:46 pm

    No, I don’t think Matlab has exactly what you are looking for.

    The best answer depends on if your primary concern is due to the amount of typing required to pass the parameters around, or if you are concerned about memory usage as you pass around large datasets. There are a few approaches that I have taken, and they all have pros and cons.

    1. Parameters in a structure. You’ve already mentioned this, in your question,but I would like to reinforce it as an excellent answer in most circumstances. Instead of calling my parameter structure paramStruct, I usually just call it p. It is always locally scoped to the function I’m using, and then I need to type p.value instead of value. I believe the extra two characters are well worth the ability to easily pass around the complete set of parameters.

    2. InputParser. I use the InputParser class a lot when designing functions which require a lot of inputs. This actually fits in well with (1), in that a sturcture of parameters can be passed in, or you can use ‘param’/value pairs, and yuo can allow your function to define defaults. Very useful.

    3. Nested functions can help in limited cases, but only when all of your functions can be defined hierarchically. It limits code re-use if one of your inner function can be generic enough to support multiple parent functions.

    4. Pass by reference using handle classes. If your concern is memory. the new form of
      classes actually allows you to define a paraemter structure which is passes by reference. The code looks something like this:

      classdef paramStructure < handle
          properties (SetAccess = public, GetAccess = public)
              param1 = [];  
              param2 = [];
              % ...
              param100 = [];
          end
      end
      

      Then you can create a new pass-by-reference structure like this

      p = paramStructure;
      

      And set values

      p.param1 = randn(100,1);
      

      As this is passed around, all passing is by reference, with the pros (less memory use, enables some coding styles) and cons (generally easier to make some kinds of mistakes.

    5. global variables. I try really hard to avoid these, but they are an option.

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

Sidebar

Related Questions

I have some scientific image data that's coming out of a detector device in
I am writing a scientific code in c which requires numerous tensor products, and
I have written a scientific GWT application that basically just asks for parameters, performs
I am developing a scientific application used to perform physical simulations. The algorithms used
I have a scientific application for which I want to input initial values at
I'm developing an image processing project and I come across the word occlusion in
For a scientific application I want to design an input form which lets the
I have a local scientific application that uses threads to process independent pieces of
I'm developing a scientific application that has to estimate (as best as possible) the
My job is mainly in high-performance 'scientific' computing. I've been doing that for ~15

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.