get rid of NaN elements in Simulink

52 views (last 30 days)
Owen
Owen on 30 Sep 2012
Commented: Image Analyst on 3 Apr 2015
Hi,
I have a special work for Simulink with which Simulink is maybe overloaded.
Many vectors of length 10 shall be processed by a model. The vectors can have all the elements as ‘NaN’ or part of elements as ‘NaN’.
Example:
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 2 3 4 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN
...
This model should get rid of all ‘NaN’ and create a new vector with a fixed length.
Example:
1 2 3 4
5 6 7 8
.
Is it possible with Simulink at all?
As far as I know, I can make it something like that in Matlab
vec(isnan(vec)) = [];
but this method is difficult to integrate in Simulink.
Thanks Senmeis

Answers (6)

Azzi Abdelmalek
Azzi Abdelmalek on 30 Sep 2012
Edited: Azzi Abdelmalek on 30 Sep 2012
if the length of your output is fixed. you can use Embedded Matlab Function or Matlab Fcn block from Simulink/Users-Defined-Functions
Some functions are not allowed by embedded function (now called Matlab Function) while they are allowed by Matlab Fcn (now called Interpreted Matlab Function)

Image Analyst
Image Analyst on 30 Sep 2012
Edited: Image Analyst on 30 Sep 2012
Try this:
m = [NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 2 3 4 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN]
% Find out where the nans are living.
nanMap = isnan(m)
% Find out which rows are all nans in every column.
allNanRows = sum(nanMap, 2) == size(m, 2)
% Get our output
mOut = m; % Make a copy.
mOut(allNanRows, :) = []
In the command window:
m =
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 2 3 4 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN
nanMap =
1 1 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1
allNanRows =
1
0
1
0
mOut =
1 2 3 4 NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN
Note: since there might be variable numbers of Nan, and in different columns, you can't get rid of them in the columns unless there are all the same number of nan's in each row. Add this code:
nanLocations = isnan(mOut)
numberOfGoodColumns = sum(~nanLocations(1,:))
numberOfRows = size(mOut, 1);
mOut(nanLocations) = [] % Erase all nans
mOut = reshape(mOut, [numberOfRows, numberOfGoodColumns])
mOut =
1 2 3 4
5 6 7 8
Though, I'm sure someone will distill that down into one compact and cryptic line with arrayfunc().

Owen
Owen on 2 Oct 2012
Thank you you both.
I must add that the vectors come one after another, which means, my model shall process only one vector at a time. If all the elements in this vector are 'NaN', then this vector shall be thrown away. If part of elements in this vector is 'NaN', then this part shall be thrown away.
As for Matlab Fcn, I think it requires an expression such as "sin". The command "vec(isnan(vec)) = [];" is not an expression because of '=' so it cannot be integrated into Matlab Fcn.
Thanks Senmeis
  3 Comments
Mike Hosea
Mike Hosea on 2 Oct 2012
It's called a "MATLAB Function Block". It was previously known as "Embedded MATLAB Block". Note that if the vectors are processed one at a time, it would be better to process them one at a time in the MATLAB Function Block because it is compiled. That changes the problem quite a bit. If the non-NaN elements are always first, then you can just check if isnan(v(1)) and then do whatever for this "throw it away" case. If the non-NaN values are sprinkled around, you can use all(isnan(v)) or any(~isnan(v)) or count them first and then create a vector and copy them in, etc., etc. I'd need some details to write the code, but the point is that in a MATLAB Function Block, you just write loops to your heart's content because the thing works by generating C code and compiling it.

Sign in to comment.


Owen
Owen on 4 Oct 2012
Thank you.
All non-NaN elements come before NaN elements, but the number of non-NaN elements can be variant. The output length is 10.
I didn’t try Embedded Matlab Block before, but I worry there may be problems with sample time. In Simulink all simulation steps are synchronous with the sample time. What happens to the sample time if all the NaN elements are thrown away? Can the output sample time be adapted automatically?
Thanks Senmeis
  8 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 7 Oct 2012
Edited: Azzi Abdelmalek on 7 Oct 2012
What I suggest is to output 4 data + 1 control data
example
input=NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
output_data= NaN NaN NaN NaN, control_data=0
input = 1 2 3 4 NaN NaN NaN NaN NaN NaN
output_data= 1 2 3 4, control_data=1
Azzi Abdelmalek
Azzi Abdelmalek on 8 Oct 2012
Owen, did you think about my suggestion?

Sign in to comment.


Owen
Owen on 8 Oct 2012
I tried following code in a Embedded MATLAB Function block.
function y = fcn(n)
i1 = 0;
var = isnan(n);
len = length(var);
for i = 1:len
if var(i) == 0
i1 = i1+1;
end;
end;
% get rid of NaN elements in a vector
if i1 ~= len
y = u(1:i1);
% if all elements are NaN, make it an empty vector
else
y = [];
end;
The problem is, the NaN vectors still exist, but I want to eliminate them. It seems impossible in Simulink.
Thanks Senmeis

MUHAMMAD  ADNAN
MUHAMMAD ADNAN on 3 Apr 2015
Edited: MUHAMMAD ADNAN on 3 Apr 2015
Dear all please help me . I am working with Neural Network toolbox . The problem is that variable valTargets,trainTargets and test Targets has Min and Max value is NaN . I'm importing a Excel data, but once it reads and imports the data, the variable's mins and maxs are NaN. Why is MATLAB giving me this error? How can I solve the NaN value to exact values. All other variable work accurate like input,output,trainperformance ,valperformance.Thanks in Advance. Name value Min Max testTargets 1x4142 double NaN NaN trainTargets 1x4142 double NaN NaN valTargets 1x4142 double NaN NaN Thanks
  1 Comment
Image Analyst
Image Analyst on 3 Apr 2015
I suggest you start your own new question rather than posting your question as an "Answer" to this one.

Sign in to comment.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!