How do you convert a script to a function?
4 views (last 30 days)
Show older comments
Mattis Roost
on 10 Aug 2021
Answered: Kshitij Chhabra
on 13 Aug 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 Comment
DGM
on 11 Aug 2021
Edited: DGM
on 11 Aug 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.
Accepted Answer
Kshitij Chhabra
on 13 Aug 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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!