How do you convert a script to a function?

8 次查看(过去 30 天)
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 个评论
DGM
DGM 2021-8-11
编辑:DGM 2021-8-11
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.

请先登录,再进行评论。

采纳的回答

Kshitij Chhabra
Kshitij Chhabra 2021-8-13
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Instrument Control Toolbox Supported Hardware 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by