I have a main function, in which I call a simulink model "test.mdl" using the following code:
%-------------------------------------------------
[t,x,y] = sim('test',timespan,options)
%-------------------------------------------------
In the "test.mdl", I build an embedded matlab function block.
%-------------------------------------------------
function dv = dynapf(a, b, c, d, e)
%-------------------------------------------------
The input parameters are produced by my main function, in which 'a' and 'b' are complex vectors, 'c' is a normal vector, 'd' and 'e' are scalars.
I have checked the help and tried two approaches:
1. Use the "From Workspace" block, change the name in "data" field into 'a','b'...respectively, connect these blocks with the corresponding input ports of the embedded matlab function block . When I run the whole program, there is an error message indicating these parameters are not exist.
2. Modify the "Configuration Parameters -->Data Import/Export". In the "Load from workspace area", check "Input" and fill in the box with "[a, b, c, d, e]", but I don't know how to do next.
In summary, my problem is how to transfer these parameters in the matlab base workspace to my embedded matlab function in Simulink model?
I would be very grateful if anyone can give me any suggestions.
the 'from workspace' block is the one i would expect to work... but let me guess, you say you have a 'main function' that calls the simulation. is it really a function or just a script? if it is a function then you would have to do assignin or evalin to put the variables into the base workspace. simulink can only access the base workspace, not a function's workspace.
... unless, I believe, you were to use the SIMSET function to set the
SrcWorkspace option.
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/simset.html
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/sim.html#brjt67g
--
Steve Lord
sl...@mathworks.com
I use a function not just a script.
I did as you said, adding the script "assignin('base','a',a)" in my main function and therefore copy 'a' to Matlab base workspace, then run the model. This time another error message appears:
"Invalid matrix-format variable specified as workspace input in 'test/From Workspace'. The matrix must have two dimensions and at least two columns. Complex signals of any data type and non-double real signals must be in structure format. The first column must contain time values and the remaining columns the data values."
It indicates me to specify the time (the first column), but 'a' is just a static matrix which is not depent on time. How should I do then?
sorry, i wasn't quite awake when i wrote that i guess. for constant values use a constant block instead of a 'from workspace' block. 'from workspace' assumes you have a time/value array and wants to read it like a signal generator. in the constant block just change the number to the workspace variable name. the assignin is still the first step anyway, just use a different block for constants.
> ... unless, I believe, you were to use the SIMSET function to set the
> SrcWorkspace option.
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/simset.html
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/sim.html#brjt67g
>
> --
> Steve Lord
> sl...@mathworks.com
----------------------------------------------------------------------------------------------
Steve, thanks very much for your information.
I have concluded the following script in my main function:
options = simset('SrcWorkspace', 'current');
I'm still debugging, hope it works.
> sorry, i wasn't quite awake when i wrote that i guess. for constant values use a constant block instead of a 'from workspace' block. 'from workspace' assumes you have a time/value array and wants to read it like a signal generator. in the constant block just change the number to the workspace variable name. the assignin is still the first step anyway, just use a different block for constants.
---------------------------------------------------------------------------------
I use the constant block instead of the 'From Workspace' block and it works! Thanks very much David!