Interfacing the Matlab API with a Matlab GUI?

Hey guys,

I’m trying to interface the matlab frontpanel API with a matlab GUI. Namely, I’d like to be able to use the setwireinvalue and updatewireins functions using the keypressfcn and keyreleasefcn the matlab GUI offers.

Does anyone know how to incorporate the opal kelly matlab API functions into a GUI callback? I tried doing something like the following, but get errors telling me the variable ‘xem’ is undefined, even though it is defined in the workspace.

[CODE]function add_pb_Callback(hObject, eventdata, handles)
% hObject handle to add_pb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

setwireinvalue(xem, hex2dec(‘00’), hex2dec(‘0001’), bitmask);
updatewireins(xem);

guidata(hObject, handles);[/CODE]

Later on in the GUI code I call this function using a keypress, but the problem seems to be with the function definition itself.

Anyone know or have any examples of how to do this?

Thanks.

I think the namespace within a function is local, so the function does not have access to anything that is not specifically passed to it. You’ll need to pass in an instance of the ‘xem’ if you wish to reference it within the function.

Do you know how to ‘pass’ an instance of xem into the function?

Do I need to define it somewhere else in the GUI code, and if so how do I need to define it?

Thanks.

Alright, I figured it out. If anyone is interested in the future my functions look something like this:

[CODE]function fwd_pb_Callback(hObject, eventdata, handles)
%Init stuff
xptr = calllib(‘okFrontPanel’, ‘okUsbFrontPanel_Construct’);
xem.ptr = xptr;
sn = ‘jwQkOcdasz’;
bitmask = hex2dec(‘ffff’);

[ok_ErrorCode, voidPtr, opened_sn] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_OpenBySerial’, …
xptr, strcat(sn)); %convert char array to string

%Set address value for move forward
setwireinvalue(xem, hex2dec(‘00’), hex2dec(‘0001’), bitmask);
updatewireins(xem);[/CODE]

I found xptr, xem.ptr, sn, and bitmask were the only required strings to put in the function in order to use the setwireinvalue and updatewireins function.

In the varargout_yourGUIName_Outputfcn function I included the following code, which automatically initializes the front panel interface and loads the frontpanel matlab functions when you start the GUI.

[CODE]
%If a different machine is used these paths will need to be changed
%To ensure the correct functions and .dlls are found
addpath(‘C:…\Matlab API’);
addpath(‘C:…l\FrontPanel\API’);
addpath(’.\okusbfrontpanel’);

% from ok_getdevicelist:
if ~libisloaded(‘okFrontPanel’)
loadlibrary(‘okFrontPanel’, ‘okFrontPanelDLL.h’);
end

% Try to construct an okUsbFrontPanel and open it.
xptr = calllib(‘okFrontPanel’, ‘okUsbFrontPanel_Construct’);
num = calllib(‘okFrontPanel’, ‘okUsbFrontPanel_GetDeviceCount’, xptr);
for j=1:num
[m,voidPtr] = calllib(‘okFrontPanel’, ‘okUsbFrontPanel_GetDeviceListModel’, xptr, j-1);
[voidPtr,sn] = calllib(‘okFrontPanel’, ‘okUsbFrontPanel_GetDeviceListSerial’, xptr, j-1, ’ ', 10);
if ~exist(‘snlist’, ‘var’)
mlist = m;
snlist = sn;
else
mlist = [mlist;m];
snlist = char(snlist, sn);
end
end
% end from ok_getdevicelist

% the OK matlab scripts assume/require the syntax: ‘obj.ptr’!
xem.ptr = xptr;

%---------------------------------------------------------------------------
% init stuff
[ok_is_enabled, voidPtr] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_IsFrontPanelEnabled’,xptr);
[ok_is_FP3sup, voidPtr] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_IsFrontPanel3Supported’,xptr);
[ok_is_open, voidPtr] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_IsOpen’,xptr);
[ok_ErrorCode, voidPtr, opened_sn] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_OpenBySerial’, …
xptr, strcat(sn)); %convert char array to string
[ok_HDL_load, voidPtr, HDLfile] = calllib(‘okFrontPanel’, ‘okUsbFrontPanel_ConfigureFPGA’,xptr, …
‘C:\lab\acassidy\projects\if_neuron\xil\prj_top_d.0717c.bit’);
[ok_is_open, voidPtr] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_IsOpen’,xptr);
[ok_is_enabled, voidPtr] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_IsFrontPanelEnabled’,xptr);
[ok_is_FP3sup, voidPtr] =calllib(‘okFrontPanel’, ‘okUsbFrontPanel_IsFrontPanel3Supported’,xptr);

%---------------------------------------------------------------------------
% useful values
bitmask = hex2dec(‘ffff’);

%---------------------------------------------------------------------------
% Init
fprintf(‘Initializing FPGA…\n’);[/CODE]