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

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 件のコメント

Paulo Silva
Paulo Silva 2011 年 3 月 29 日
please provide more details, code, etc
cyberdyne
cyberdyne 2011 年 3 月 30 日
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.

サインインしてコメントする。

回答 (5 件)

Seth Popinchalk
Seth Popinchalk 2011 年 3 月 29 日

0 投票

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
Harsha Vardhan Rao Avunoori 2011 年 3 月 29 日

0 投票

Your simulation should come to a halt if you want to export data to workspace....or try using the S-function block....
cyberdyne
cyberdyne 2011 年 3 月 30 日

0 投票

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 2011 年 3 月 30 日

0 投票

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 件のコメント

cyberdyne
cyberdyne 2011 年 3 月 30 日
Therefore i have to write, in "Override input port properties":
block.InputPort(1).Dimensions = 2; %if i want to read the full matrix
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = true;
Correct?
Kaustubha Govind
Kaustubha Govind 2011 年 3 月 30 日
Yes, if those are the properties of the signal being logged.
cyberdyne
cyberdyne 2011 年 3 月 30 日
And the properties of input of FromFile block just are those i wrote, are they ?
Kaustubha Govind
Kaustubha Govind 2011 年 3 月 31 日
I don't think I understand your question - why are you using a From File block?
cyberdyne
cyberdyne 2011 年 3 月 31 日
So i've not understood your answer, sorry :(
Can you write the entire code (my code+your code) please?
Thanks for patience!!!

サインインしてコメントする。

cyberdyne
cyberdyne 2011 年 3 月 31 日

0 投票

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 件のコメント

Kaustubha Govind
Kaustubha Govind 2011 年 3 月 31 日
You cannot load file.mat because it does not exist at this point of the simulation. You connect your signal (which was acting as input to the To File block) directly to the input of this S-function. You do not need any To/From File blocks anymore.
cyberdyne
cyberdyne 2011 年 3 月 31 日
Ok. I've understood it. But in the code above i've done that you tell (as you can see, i've assigned mat-file variable to block input), but it returns the same error.
Kaustubha Govind
Kaustubha Govind 2011 年 4 月 1 日
Could you paste the exact line that the error message is pointed to?
cyberdyne
cyberdyne 2011 年 4 月 3 日
it's this:
block.OutputPort(1).Data = a.ans3(1,J);
Kaustubha Govind
Kaustubha Govind 2011 年 4 月 4 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGeneral Applications についてさらに検索

質問済み:

2011 年 3 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by