To workspace Block and To Workspace Signal don't exist in workspace

7 views (last 30 days)
Hi,
I don't understand why i can't assign simout or yout (default name of To Worspace block's variable) as variables in m.file editor after i started simulink simulation.
  2 Comments
cyberdyne
cyberdyne on 30 Mar 2011
The problem is the same of my recent posts. I try to avoid it by different ways.
I've this m.file in the workspace where i need "f" have to be the floating value of a signal in simulink model. I need also to send ts to model.
clear all
a=load('file.mat'); %load mat file where there is the past simulation signal.
b=a.ans; %assign to "b" the matrix
[I,J] = find(abs(b-(f)) < 0.001); %i need to find indexes of the nearest element of matrix
ts=b(1,J); %it get time value (1st row of the matrix) where both past simulation signal (from file.mat) and new signal's floating value respect abs(b-(f)) < 0.001. But I need to send this ts somewhere to simulink model.

Sign in to comment.

Answers (5)

Seth Popinchalk
Seth Popinchalk on 29 Mar 2011
Variables assigned to the workspace do not appear until the model is paused or stopped. If you are trying to process the data from the simulation while the simulation is running, it is better to do that from a MATLAB S-function.

Harsha Vardhan Rao  Avunoori
Your simulation should come to a halt if you want to export data to workspace....or try using the S-function block....

cyberdyne
cyberdyne on 30 Mar 2011
I build following S-function:
function name(block) %name is MATLAB-file name
setup(block);
function setup(block)
% Register number of ports
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Override input port properties
block.InputPort(1).Dimensions = 1;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = true;
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
% Register parameters
block.NumDialogPrms = 0;
block.SampleTimes = [0 0];
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Terminate', @Terminate); % Required
function Outputs(block)
a=load('file.mat'); %"file" is the file name of past simulation signal
[I,J] = find(abs(a.ans5(2,:)-(block.InputPort(1).Data)) < 0.001); %where "ans5" is the variable name in the file.mat
block.OutputPort(1).Data = a.ans5(1,J);
function Terminate(block)
It returns the error: "Error evaluating registered method 'Outputs' of M-S-Function 'name' in 'untitled1/Level-2 M-file S-Function1'. Invalid assignment in 'untitled1/Level-2 M-file S-Function1': attempt to assign a vector of width 0 to a vector of width 1"
Where is the mistake(s) ?

Kaustubha Govind
Kaustubha Govind on 30 Mar 2011
Again, as Seth pointed out, file.mat is not available until after the simulation is paused/stopped. Instead, feed the signal that you are logging as input to the S-function.
function Outputs(block)
a = block.InputPort(1).Data;
% your code here
  5 Comments
Kaustubha Govind
Kaustubha Govind on 31 Mar 2011
I don't think I understand your question - why are you using a From File block?
cyberdyne
cyberdyne on 31 Mar 2011
So i've not understood your answer, sorry :(
Can you write the entire code (my code+your code) please?
Thanks for patience!!!

Sign in to comment.


cyberdyne
cyberdyne on 31 Mar 2011
I tried with this code:
function s_function(block)
setup(block);
function setup(block)
% Register number of ports
block.NumInputPorts = 2;
block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Override input port properties
block.InputPort(2).Dimensions = 1;
block.InputPort(2).DatatypeID = 0; % double
block.InputPort(2).Complexity = 'Real';
block.InputPort(2).DirectFeedthrough = true;
% Override output port properties
block.OutputPort(1).Dimensions = 1;
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
% Register parameters
block.NumDialogPrms = 0;
block.SampleTimes = [0 0]; % <<<<--------------------- SAMPLE TIME
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Terminate', @Terminate); % Required
function Outputs(block)
a=load('file.mat');
a.ans3 = block.InputPort(1).Data;
[I,J] = find(abs(block.InputPort(1).Data-(block.InputPort(2).Data)) < 0.001); %where "ans3" is the variable name in the file.mat
block.OutputPort(1).Data = a.ans3(1,J);
function Terminate(block)
But it returns the same error: "Invalid assignment in 's/Level-2 M-file S-Function1': attempt to assign a vector of width 0 to a vector of width 1". I don't understand. It appears like a simple work. I have only to take time value of file.mat signal when an element of the 2nd row is equals to input signal. :(
  5 Comments
Kaustubha Govind
Kaustubha Govind on 4 Apr 2011
Maybe there is no index "J" in the variable a.ans3? I would recommend setting a breakpoint at this line and debug to see if you are indexing into the variable correctly.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!