I am getting "Undefined function or variable" for an existing variable.

9 views (last 30 days)
Hello,
I am working on a script that calls another script, QuickSIN_parameters.m, which in turn creates the 'parameters' structure, below. However, every time I run it, it generates a "Undefined function or variable" error for the variable "parameters".
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @SpeechTest_OpeningFcn, ...
'gui_OutputFcn', @SpeechTest_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
function SpeechTest_OpeningFcn(hObject, ~, handles, varargin)
%% Checks that the correct experiment name was entered.
if ~strcmp(varargin{2},'QuickSIN')
disp('Wrong experiment name! Enter QuickSIN as the experiment name.')
error('Incorrect input to SpeechTest function.')
end
%% Set parameters
handles.parname = varargin{2}; % experiment name
run QuickSIN_parameters.m % load experiment parameters
handles.par = parameters; % structure of parameters
...
It produces this error when going line-by-line in debug mode as well. What is bizarre, though, is that if I debug and then run the 'handles.par = parameters;' line in the command window, it will run without error. I have tried troubleshooting with the advice from previous questions along this line Why do I get the error "Undefined function or variable"?, but I am still lost. I would appreciate any insight that you could provide. Please let me know if there is anything else that I can provide for clarity- this script is very large, so I was not sure how much context it would be prudent to include.
For context, I am running this script on MATLAB 2016b.
Thank you,
Andrew
  3 Comments
Stephen23
Stephen23 on 7 Oct 2021
Edited: Stephen23 on 7 Oct 2021
"I would appreciate any insight that you could provide. "
Fragile code design is the ultimate cause of the problem, right here: "... a script that calls another script..."
Writing/calling functions and reliably passing data as input/output arguments goes a long way to making code more robust against such errors:
Andrew Levitsky
Andrew Levitsky on 7 Oct 2021
I completely agree. Unfortunately, I am using another lab's code for an experiment and am simply trying to put out fires. It had run for previous subjects without error, we do not know what changed.

Sign in to comment.

Answers (1)

per isakson
per isakson on 6 Oct 2021
Edited: per isakson on 6 Oct 2021
Run this modified script from the command window
whos par*
run QuickSIN_parameters.m % load experiment parameters
whos par*
handles.par = parameters; % structure of parameters
and study the two outputs of whos par*. You expect parameters to exist in the second list and not in the first. Is that the case?
Scripts are executed in the base workspace (in fact in the callers workspace). In your case a variable, parameters, may or may not exist before you run your script. That's why one see so many scripts starting with the lines
clc
clear all
  4 Comments
Andrew Levitsky
Andrew Levitsky on 7 Oct 2021
Edited: Andrew Levitsky on 7 Oct 2021
Apologies, that was an error on my part- it was meant to say "parameters". I have changed it in the original question.
I ran whos again and unfortunately got the same result.
Name Size Bytes Class Attributes
parameters 1x1 2274 struct
I have added a bit more context of the code into the original question as well.
per isakson
per isakson on 8 Oct 2021
Edited: per isakson on 8 Oct 2021
Your code snippet is from an application created with GUIDE.
I failed to reproduce the issue you encounter
  1. created an app with GUIDE, based on the template "GUI with Uicontrols"
  2. added the code below at the end of the local function *_OpeningFcn
run QuickSinScript.m % creates parameters
disp( parameters )
handles.par = parameters;
If disp shows parameters correctly and the following assignment throws the error, we need to politely ask @Walter Roberson for help.
"this script is very large" with scripts one doesn't know which variables of the calling workspace are used and which new variables are created. Does QuickSIN_parameters include logics that could explain that parameters isn't always created (far fetched)? Early returns are allowed in scripts.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!