How do you convert a script to a function?

조회 수: 1 (최근 30일)
Mattis Roost
Mattis Roost 2021년 8월 10일
답변: Kshitij Chhabra 2021년 8월 13일
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에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by