MATLAB add_line with for loop gives invalid Simulink object name error - matlab

I have MATLAB Simulink model where "Subsystem" is connected with "Add" block. I want to add Data type conversion block in between Subsystem and Add block using script. I implemented as shown in the code below. But I am getting error Invalid Simulink object name in the add_line of the code.
for i = 1:n;
delete_line('myModel',strcat('Subsystem/',num2str(i),'/'), strcat('Add/',num2str(i)));
add_block('simulink/Commonly Used Blocks/Data Type Conversion', strcat('myModel','/Data Type Conversion',num2str(i)));
add_line('myModel', strcat('Subsystem/',num2str(i),'/'), strcat('Data Type Conversion',num2str(i)),'autorouting','on');
add_line('myModel',strcat('Data Type Conversion',num2str(i),'/1'),strcat('Add',num2str(i)),'autorouting','on');

You aren't specifying the destination port number for the Data Type Conversion block.
Try
add_line('myModel', strcat('Subsystem/',num2str(i),'/'), strcat('Data Type Conversion',num2str(i),'/1'),'autorouting','on');
You may also need to do this for the line to the add block as I'm guessing it has multiple ports too.

Related

Matlab tcpclient function error

trying to use matlab tcp communication feature. When i use t = tcpclient('169.254.131.130', 10300) i get Undefined function 'tcpclient' for input arguments of type 'char'. Even i try to use the same function as tcpclient(host,port) i still get same error.I use the function from https://www.mathworks.com/help/matlab/tcpip-communication.html. What is the problem? In website it says this function is used in this configuration type.

Does matlab treat colon mark differently during variable assignment and indexing without assignment?

For example I have a 1*30 structure a.field, when I type a(:).field in command window it just iteratively display a(1).field, a(2).field,... However, when I was trying to assign a(:).field to another variable b, what b get is just a(1).field.
BTW, if I attampt to pass a(:).field to a function, Matlab just throws an error "too many input arguments".
What is the mechanism behind? My guess is that matlab threat colon equivlant to the first element during assignment, is that true?
You need to add brackets, otherwise matlab don't understand that your trying to store an array:
b = [a(:).field]
Another option that provide similar result:
b = horzcat(a(:).field)

Specify port types in s-function blocks

I'm building my first s-function block from compiled C code. Everything is going fine, except that the s-function block demands that its interface variables are of type double, even though the underlying C interface variables are not. The block raises an error if I connect a boolean signal to the input and try to run.
I'm getting the variables in the code by calling ssGetInputPortSignal and ssGetOutputPortSignal, and casting the void pointers they return into the correct pointer types.
How do I configure the types of an s-function block's parameters in Simulink?
Take a look at ssSetInputPortDataType. Needs to be called in the S-Function mdlInitializeSizes function. In your case you'll need something like:
ssSetInputPortDataType(S, 0, SS_BOOLEAN);
Assuming the input port is the first one and you're not concerned about the return value.
The function for the outputs is ssSetOutputPortDataType, with identical use.

not able to define function in matlab showing error

I defined the following function in matlab:
function draw_snake(snake,food)
but matlab shows me an error message saying:
Error: Function definitions are not permitted in this context.
I am new to matlab and just installed it.
I am using MATLAB version 8.1.0.604.
See here
The docs say
"function [y1,...,yN] = myfun(x1,...,xM) declares a function named
myfun that accepts inputs x1,...,xM and returns outputs y1,...,yN.
This declaration statement must be the first executable line of the
function."
So,
function draw_snake(snake,food)
must be the first line in a script called draw_snake.m

How can I read data from COM object (an activex server) in MATLAB?

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."