I am getting "Undefined function or variable" for an existing variable.
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Stephen23
il 7 Ott 2021
Modificato: Stephen23
il 7 Ott 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:
Risposte (1)
per isakson
il 6 Ott 2021
Modificato: per isakson
il 6 Ott 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 Commenti
per isakson
il 8 Ott 2021
Modificato: per isakson
il 8 Ott 2021
Your code snippet is from an application created with GUIDE.
I failed to reproduce the issue you encounter
- created an app with GUIDE, based on the template "GUI with Uicontrols"
- 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.
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!