How do you convert a script to a function?

8 visualizaciones (últimos 30 días)
Mattis Roost
Mattis Roost el 10 de Ag. de 2021
Respondida: Kshitij Chhabra el 13 de Ag. de 2021
Hello everyone,
I am fairly new to Matlab and I seem to don't understand a crucial detail about functions. I have the following piece of code that establishes a connection to my PicoScope 2204A:
%% Load Configuration Information
PS2000Config;
%% Device Connection
% Create a device object.
ps2000DeviceObj = icdevice('picotech_ps2000_generic.mdd');
% Connect device object to hardware.
connect(ps2000DeviceObj);
The code works perfectly fine. But when I try to put it inside a function (I want to use this as a callback function for a GUI made with App Designer) like this:
function startPicoScope
%% Load Configuration Information
PS2000Config;
%% Device Connection
% Create a device object.
ps2000DeviceObj = icdevice('picotech_ps2000_generic.mdd');
% Connect device object to hardware.
connect(ps2000DeviceObj);
end
I get the following error:
>> Error using icdevice/connect (line 114)
>> An error occurred while executing the driver connect code.
>> Undefined variable "ps2000Enuminfo" or class "ps2000Enuminfo.enPS2000Range.PS2000_50MV".
>> If this error is not an instrument error, use MIDEDIT to inspect the driver.
I am pretty sure it has something to do with how variables get stored inside functions, but I can not figure out how to fix this.If anyone of you knows the answer to my struggle I would really appreciate your help.
Cheers,
Mattis
  1 comentario
DGM
DGM el 11 de Ag. de 2021
Editada: DGM el 11 de Ag. de 2021
I don't have ICT and am totally unfamiliar with how these driver objects are used. I'm also unfamiliar with AppDesigner conventions, as it doesn't run in my environment. At a distance, it seems to me like this is an issue of variable scope. At least with GUIDE and programmatic GUIs, the functions called by the GUI code are all operating in the GUIs own workspace and don't have direct access to things in the base workspace. There are some similar questions that point in that direction.
I don't know if that's of any help though.

Iniciar sesión para comentar.

Respuesta aceptada

Kshitij Chhabra
Kshitij Chhabra el 13 de Ag. de 2021
From my understanding, you are facing this issue because the function and base workspaces have a different scope. The Instrument Control drivers pulls the information from the base workspace and thus you might be getting this error.
For reference you can refer to this MATLAB Answer.
Further, to use it as a callback you can refer to the MATLAB Documentation here.

Más respuestas (0)

Categorías

Más información sobre Instrument Control Toolbox Supported Hardware en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by