Error when trying to run a parfor loop from inside a GUI

9 views (last 30 days)
I've created a GUI using a figure and several uicontrol objects (checkboxes, pushbuttons, radios, edits,..). The idea is to use this as a dashboard to run a user-decided subset of functions. When a certain button is pushed, it gets information from the values of GUI checkboxes and radio buttons and uses this information as the input to a function called myFunction. myFunction then has a switch format, which operates based on some of the values input from the GUI.
For one of the switch values, I'd like to run a function, myFunction2, which is an I/O function (it looks like output(loopcount)=myFunction2(filename, otherinput(loopcount)) ). myFunction2 navigates to a data file based on an input to the function, filename. Filename contains the complete path to the data file. I want to run this I/O function several times on the same data file with the "otherinput" varying for each execution.
This runs successfully when I run it as a for loop:
function myFunction(gui_input)
switch gui_input
case Mode_1
for loopcount = 1:1000
output(loopcount) = myFunction2(filename, otherinput(loopcount))
end
end
end
However, when I try to run this as a parfor loop (Start parallel pool from main Matlab window before I start up the GUI) and simply change "for" to "parfor" I get this error:
Error using myFunction>(parfor body) (line 29)
An UndefinedFunction error was thrown on the workers for 'filename'. This might be because the file containing 'filename' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.
Error in myFunction (line 28)
parfor loopcount = 1:10
Error in gui_function/Runanalysis_callback (line 154)
myFunction(gui_input)
Caused by:
Undefined function or variable 'filename'.
Error while evaluating uicontrol Callback
Unfortunately I'm pretty new to both GUI's and parfor loops. Can anyone point me in the right direction?
Thanks, I would really appreciate it!

Accepted Answer

Kristin Busa
Kristin Busa on 4 May 2015
For interested readers, I believe I've solved this issue.
The variable "filename" was coming in as a variable in a structure within "gui_input". I had used the file exchange code v2struct to unpack the structure gui_input. This makes the variable "filename" available in the workspace (verified with the debugging feature). However, when I run in parfor loop, Matlab does not seem satisfied that this variable exists, as its name is never actually mentioned in myFunction before I call it as an input in the parfor loop. I found that simply including "filename = filename" after the v2struct command and before the parfor loop has solved my problem. Not very elegant but it worked!

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!