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

  • Home
  • SEARCH
  • 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 8452485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:30:43+00:00 2026-06-10T11:30:43+00:00

Inside a parfor loop, I am trying to call a function that accesses a

  • 0

Inside a parfor loop, I am trying to call a function that accesses a global to no avail.

The function

function a = getA()
   global OPTIONS;
   a=OPTIONS.PROBLEM.A;
end

The loop:

parfor i=1:3
    b=getA();
end

The error:

Error using parallel_function (line 589)

Attempt to reference field of non-structure array.

What am I doing wrong?

  • 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-10T11:30:44+00:00Added an answer on June 10, 2026 at 11:30 am

    From the documentation on parfor:

    The body of a parfor-loop cannot contain global or persistent variable declarations.

    In the context of your problem, i.e., calling a function within the parfor that in turn references a global, this translates into: “parfor will probably not give expected or meaningful results”.

    This makes perfect sense. Consider the following

    Lab 1:         Lab 2: 
    
    GetB();        GetB();
    

    if the contents of GetB() is this:

    function GetB()
        global B;
    
        %# do something useful
    
        B = rand;
    
     end
    

    what will the value of B be when it is referenced on Lab 1? and on Lab 2? How are the different outcomes of rand communicated? It’s going to be a mess!

    Writing code suited for parfor loops can be a real pain when that code comes from something that only had normal for-loops in mind. Generally, when you know beforehand you’re going to write a computationally intensive piece of Matlab code, write all functions and loops as parfor loops right from the beginning. That is the only way that bugs like these will not cost you a day on transcoding your functions.

    Converting from for to parfor is not at all trivial.

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

Sidebar

Related Questions

Inside the initialize() function there is a jQuery each loop. Inside that loop is
Inside a function I have created, I have this loop that checks if the
Inside my Controller i have function that runs after user clicks on item, which
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
Inside the Application_Start of my Global.asax.cs, I am trying to get the current application
Inside of a trigger I'm trying to loop over all columns on a table
Inside a function I need to test if another variable function exists and then
inside show.js.erb $('#viewport').html(<%= escape_javascript(render('show')) %>); I am trying to render the show view into
Inside Vim on Windows, I'm trying to filter the lines in a file through
Inside a function of mine I construct a result set by filling a new

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.