I am trying to implement a "multithreaded" program in Matlab. It is not actually multithreaded, but it consists on a main script (A) interacting with other scripts (B,C...) in other Matlab instances via TCP/IP.
The way I tried to do this is to create a listener on the tcpip object (srv) :
func = #(src, evnt) disp('booh');
l = listener(srv, 'BytesAvailable', 'PostSet', func );
which returns me
Undefined function 'listener' for input arguments of type 'tcpip'.
I'm new to listeners in Matlab, so I might be missing something here. In case not, is there a clean way to know when data has been received ? (I prefer not having to do an infinite loop checking the value of srv.BytesAvailable).
A way to achieve this is to set the srv.BytesAvailableFcn property to a function callback.
Related
I am trying to create a simple "browse" button on a Matlab (R2016a) GUI.
My code is something like:
hd = dialog;
hb = uicontrol('parent',hd,'style','pushbutton','string','browse',...
'callback',#uigetdir);
The callback function uigetdir has 2 optional arguments STARTPATH, TITLE. In principle I could pass these on my callback by concatenating them with the function handle on a cell array, such as
hd = dialog;
hb = uicontrol('parent',hd,'style','pushbutton','string','browse',...
'callback',{#uigetdir,'myStartPath','myTitle');
Whether my browse button calls uigetdir with or without the optional arguments, it will crash. Different errors, same reason: uicontrol decides to include 2 uncalled-for, weird variables (containing UI properties) as arguments to the callback function, and uigetdir doesn't know what to do with them.
Does this mean I cannot use uigetdir (or pretty much any other built in function) as a callback function in a GUI? There must be a solution besides writing a custom function, no?
By default all uicontrol objects are passed two input arguments:
The uicontrol handle itself
An object containing information specific to the event.
When you define a callback by simply appending # to a function name to create a function handle, these two arguments are automatically passed to the function.
You can instead craft your anonymous function to accept two input arguments and call uigetdir with no input arguments, effectively ignoring the default callback inputs.
set(hb, 'Callback', #(s,e)uigetdir())
If you want to pass a start path and a title you can pass those to uigetdir from within the anonymous function.
set(hb, 'Callback', #(s,e)uigetdir('mystartpath', 'mytitle'))
I created this function to browse all the functions stored in a file and now I want to select my function in my main program how can I select it
This is my function:
function testMode(i)
a=dir('H_*.m');
if exist('i','var')
if isempty(i)
z={a.name}';
[selection,ok]=listdlg('ListString',z,'SelectionMode','single');
if ok
i=find(selection,1,'first');
end
end
nom=a(i).name;
nom=nom(1:end-2);
disp(nom)
else
disp('fonction a un argument')
end
It looks like you will have the name of the function as a string. To call that function, you can either use feval, or you can convert the string to a function handle and call that.
result = feval(nom, argument1);
or
fcn = str2func(nom);
fcn(argument1);
The latter should be preferred, as the conversion can be done once and the function handle re-used after that, and because the calling syntax is exactly the same as calling a regular function.
For more information on function handles, see doc function_handle.
Also, for a different approach, see this document: http://www.mathworks.com/help/matlab/ref/localfunctions.html. If you can create your "menu" of functions as local functions in a single file, this approach could be very clean and simple, as it will directly give you a cell array of function handles of all of those subfunctions. From there, you can get easily get the function name for display using functions command.
Matlab's DSP toolbox has a function called adaptfilt., where calling adaptfilt is not enough, but you must add the .< algorithm> where the algorithm can be one of the many things, which we can view using help adaptfilt. What kind of Matlab structure has been used here (or what is the . operator and how can I make my own function that has to be called using a dot).
Also, the result of doing, say, adaptfilt.fdaf, gives a result that seems like a structure. How can I view all the elements of this structure (that is, if there are any more members besides the values that are returned on the screen by the function itself)?
adaptfilt is a class definition, of which fdaf is a member. Then, you use the dot operator to access the static member of the class. See Static Methods in the MATLAB documentation. In summary, to define a similar function yourself use
classdef MyClass
...
methods(Static)
function y = yourFunc(x)
...
end
end
end
The result you're getting from adaptfilt.fdaf is in fact an object. The adaptfilt.fdaf documentation page outlines the members of the object.
I am trying to connect a simulator to the MATLAB. The simulator program exposes a COM object interface.
I have connected to the COM object by the following command and can perform most of it methods:
h=actxserver(ProgID)
But some of its methods need passing of a Variant* type as output.
Here is the signature of one of the methods indicated by "invoke" method:
ReadOutputImage=Variant(Pointer) ReadOutputImage(handle, int32, int32, `ImageDataTypeConstants, Variant(Pointer))`
I have called this method with several syntax's, but none of them work:
a=uint8([0]) %means unsigned integer array with 1 member
h.ReadOutputImage(0,1,2,a) % 0 ,1 ,2 are contants pointing to the position, number of elements to read and size of elemnts while 2 shows Byte element (VT_UI2 COM type).
Other syntax's that I have tried and has no result are: using uint16, uint32, int8, int16, int32 for all of the followings:
logical types (like a=[false]),
cell arrays (like a={uint8([0])} )
empty cell array {}
empty array []
empty sring ''
I have used libpointer as well:
a=libpointer;
also a=libpointer('uint8Ptr',0)
also a=libpointer('bool',false)
also a=libpointer('bool',[0])
The problem is that I am not sure about the following items:
What is the similar type of " Variant(Pointer) " in MATLAB?
What is the method of passing a variable as output to a COM method in MATLAB?
Is it even possible to get a value from a COM object method result as a pointer in MATLAB?
To find how the data appears in other clients, I have imported the same dll file into Delphi and the signature of the type library for the above method is like this:
procedure ReadOutputImage(StartIndex: Integer; ElementsToRead: Integer;
DataType: ImageDataTypeConstants; var pData: OleVariant);
Yes Siemens has provided a guide for this com server (prosim) and based on such documentation I have connected and performed most of its methods. But the methods which read I/o data are not working. In documentation the method signature is specified as follows: (in VB)
STDMETHOD(CS7Prosim::ReadOutputImage)(long startindex,long elementstoread, imagedatatypeconstants DtaType, VARIANT* pData)
What about your application, was it working? Did it contains variant pointers as the returning argument? Did you have simillar methods in that application?
Thank you
I can help with #2 in your question. I just worked through this myself. Basically, any pass by reference to COM object you to access after it is modified, Matlab just spits back as an output.
[var1 a]=thisObject.DB.Execute(queryString,a)
See here
"The syntax shown here shows a server function being called by the MATLAB client. The function's return value is shown as retval. The function's output arguments (out1, out2, ...) follow this:
[retval out1 out2 ...] = handle.functionname(in1, in2, ...);
MATLAB makes use of the pass by reference capabilities in COM to implement this feature. Note that pass by reference is a COM feature. It is not available in MATLAB at this time."
Here's a line of code that's giving me trouble.
arrayfun(#(x)container.nodelist(x).config(#a_func_handle,0),2:6);
Container is a class with one of its properties being an object array of nodes, and that array is called nodelist.
Each node has a function called config that is used to initialize it. Config expects one input, one of which is a handle to a function. The function handle I'm passing needs a constant passed along with it, which is represented by the 0.
In this case, I want to configure the nodes at positions 2 through 6 in nodelist with a specific function, so I thought to use arrayfun instead of a for loop.
Unfortunately, Matlab barfs with "too many inputs" for function config. What am I writing wrong? Is this example clear?
I figured it out. What I ended up doing was using nested anonymous functions, like so:
arrayfun(#(y)y.config(#(x)(configSlave(x,0))),exp.pico_list(2:6));
If I've understood correctly, config is a method of the objects contained within your nodelist array. In which case, in the usual MATLAB manner, the object on which you're invoking the method gets passed as the first argument. For example, you might need to write the config method like this:
function config(obj, fcnHandle, value)
obj.FunctionHandle = fcnHandle;
obj.Value = value;
end