Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 7 Okt. 2021
Bearbeitet: Stephen23 am 7 Okt. 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 am 7 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

per isakson
per isakson am 6 Okt. 2021
Bearbeitet: per isakson am 6 Okt. 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 Kommentare
Andrew Levitsky
Andrew Levitsky am 7 Okt. 2021
Bearbeitet: Andrew Levitsky am 7 Okt. 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 am 8 Okt. 2021
Bearbeitet: per isakson am 8 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by