Is it possible to use structure variable as an input parameter for Matlab function block in simulink?

I need to use a structure variable as an input in my matlab function block! I've done the instructions as said in documentation but my model doesn't run. I think structure arrays aren't supported in matlab function block, are they?

 Accepted Answer

You can create a non-virtual bus signal in simulink and pass this to Matlab function block. Inside matlab function block, bus and its elements can be used in the form of structure.
Similarly you can create a structure in Matlab function block and output it as bus signal from the block.
See
Can you describe your implementation (post code & attach image). It is hard to say why it is not working without seeing the implementation.
[Edited 1] Type
>> emldemo_bus_struct
in your command window. This will open an example model of bus. You can proceed with this example.
Anyway, which matlab/simulink version are you using ?

6 Comments

Thanks a lot for your support. Here is the code:
You should consider that parameter net is time invariant and it's class is structure.
function t = fcn(e,net)
%#codegen
net=nnet(e,net);
t = net.Output;
end
%%%this function do forward propagation and Compute output of all neurons%%%
function output=nnet(x,net)
%x is network input vector
%net is a structure array that represent the whole network
net.O(1:net.non(1),1)=x;%In the first layer, Inputs just are distributed
%%%Computing outputs for the second layer and afterwards %%%
for L=2:1:net.L
for i=1:1:net.non(L)
net.O(i,L)=neuron(net.O(1:net.non(L-1),L-1),net.W(i,1:net.non(L-1),L),net.T(i,L),net.funcname);
net.Oder(i,L)=neuron(net.O(1:net.non(L-1),L-1),net.W(i,1:net.non(L-1),L),net.T(i,L),net.funcnameder);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
net.Output(1,:)=net.O(1:net.non(net.L),net.L);
output=net;
end
%%%This function acts as a neuron%%%
%x is input vector
%W is weight vector
%theta is bias
%actfunc is neuron activation function
function O=neuron(x,W,theta,actfunc)
actfunc1=str2func(actfunc);
O=actfunc1(W*x-theta); % Compute Outputs
end
function y=sigmoid(x)
land=1;
a=1;
y=a./(1+exp(-land*x))-0./2;
end
function y=sigmoidder(x)
land=1;
a=1;
y=a*((1+exp(-land*x)+land*exp(-land*x))/(1+exp(-land*x))^2);
end
Is input signal to net from simulink a non-virtual bus?
Why are you modifying input signal 'net'
net=nnet(e,net);
Dear TAB the net represent a neural network and nnet do the forward propagation. the outputs for each layer and neuron must be collected in net. I've realized that the problem is about str2func command. Do you have any idea? If it does not supported for code generation in matlab function block do you have any suggestion to call a function which it's name has been saved in a string previously?
the net is not a signal. It's just a parameter which comes from base workspace!
I will, whether structure parameters are directly supported in Matlab function block or not.
You can not change the parameter value (from any workspace) in the model. They are constant for the model.

Sign in to comment.

More Answers (1)

Yes, structure variables are supported in Matlab function block as input parameters. But You should consider that they can't be changed during the function. they're constants. For doing this first you must define the variable as an input parameter in Ports and Data Manager. In terms of str2func command I don't know whether it's supported or not but the feval command works properly.

Categories

Products

Asked:

on 18 Dec 2012

Community Treasure Hunt

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

Start Hunting!