How do I export a vector to workspace in simulink?

조회 수: 13 (최근 30일)
Shiloh Greer
Shiloh Greer 2019년 6월 26일
편집: Jim Riggs 2019년 6월 28일
I'm trying to model a household appliance in simulink. When the simulation ends, I need the model to output a vector to the workspace which contains power loads corresponding to simulation times. I've started with trying to implement as simple a model as I can, shown below.
simulink model.PNG
The idea here is to just send simulation time to a function which appends the simulation time to a vector containing all simulation times. Basically, if the simulation stop time is 10, what I want from this model is AllTimes = [1, 2, 3, ... 10].
I've set the model to run an initialization function, which contains the following:
times = [];
The MATLAB function block contains the following:
function times = fcn(currentTime)
times = [times, currentTime];
times = time;
Simulink complains that:
simulink error 1.PNG
The issue, as I understand it, is that "time" is declared outside fcn's scope. I've tried the following tweaks to fix this:
function times = fcn(currentTime)
if ~exist('vect')
vect = [];
end
times = currentTime;
function times = fcn(currentTime)
times = evalin('base', [times, currentTime]);
times = currentTime;
function times = fcn(currentTime)
assignin('base', times, [times, currentTime])
times = currentTime;
However, none of these have worked.
I also tried importing the function times from the workspace and passing it as an argument to fcn, like so:
simulink model 2.PNG
function times = fcn(currentTime, times)
times = [times, currentTime];
times = currentTime;
However, this throws the error:
simulink error 2.PNG
Unfortunately, I have very limited knowledge of MATLAB and even less knowledge of simulink. If anyone has any advice I would very much appreciate help.
  댓글 수: 5
Shiloh Greer
Shiloh Greer 2019년 6월 28일
I just discovered that exactly what I'm trying to achieve can be done by using the "To Workspace" block and having it export an array. I have switched to that implementation, and have gotten what I needed from it.
Thank you for your help, Jim. I appreciate you being willing to help me out.
Jim Riggs
Jim Riggs 2019년 6월 28일
편집: Jim Riggs 2019년 6월 28일
Oops. I was assuming that the block in your diagram with the label "Alltimes" was a "to Workspace" block and that you were simply trying to constuct the desired output vector.
In the future, select all of your blocks, then select "Format / Show Block Name" before you capture screenshots.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jim Riggs
Jim Riggs 2019년 6월 26일
It'a actually very easy. Simulink has a block for combining two signals, called a "mux" block.
Simply combine the two signals together, like so:
MatlabAnswers 20190626.JPG
  댓글 수: 2
Shiloh Greer
Shiloh Greer 2019년 6월 26일
Hello Jim,
When I try using your solution I get the same error as the last one I put up a screenshot of. Does it matter that the times variable is a vector, and that we're combining a scalar signal with a vector signal?
Jim Riggs
Jim Riggs 2019년 6월 26일
편집: Jim Riggs 2019년 6월 26일
The two signals have to be the same type and compatible shape.
You can use a "Data type convert" block to change the type of one signal, and there is a "reshape" block if you need to change the shape.
For more complex shapes (2D or more), you may need to use use a "Vector concatenate" block in place of the Mux.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by