필터 지우기
필터 지우기

Embedded Matlab function to save a random number

조회 수: 3 (최근 30일)
Owen
Owen 2012년 10월 23일
Hi,
I want to write an Embedded Matlab function to store the ramdom number of the Random Generator block into a vector, something like 'To Workspace' block.
The code is following: function fcn(u) eml.extrinsic('assignin'); eml.extrinsic('evalin'); % index initialization persistent index; if isempty(index) index = 0; end;
% every time a new index
index = index + 1;
% save the variables in workspace
assignin('caller', 'a', u);
assignin('caller', 'b', uint16(index));
evalin('caller', 'c(uint16(b)) = a');
The variable 'c' is defined in a function which runs the 'sim()' command. c = zeros(1000, 1);
There is an error: In an assignment A(I) = B, the number of elements in B and I must be the same.
which relates to this line:
evalin('caller', 'c(uint16(b)) = a');
Could anybody help me to solve this problem?
Thanks Senmeis

채택된 답변

Kaustubha Govind
Kaustubha Govind 2012년 10월 23일
편집: Kaustubha Govind 2012년 10월 23일
Is 'u' a non-scalar value? If yes, this is expected. For example:
>> c(uint16(4)) = [0 0]
In an assignment A(I) = B, the number of elements in B and I must be the same.
If you plan to assign a vector to create a 2-D matrix C, you should probably use the colon operator:
>> c(uint16(4),:) = [1 2]
c =
0 0
0 0
0 0
1 2

추가 답변 (1개)

Owen
Owen 2012년 10월 24일
The problem is solved. The error is because the Random Number Generator block generates frame based data. A Buffer block of length 1 is inserted and everything becomes OK.
Are there any other blocks that convert nx1 vectors to 1x1 vectors besides Buffer block?
Thanks Senmeis
  댓글 수: 1
Kaustubha Govind
Kaustubha Govind 2012년 10월 24일
Does the "Random Number Generator" block have a dialog setting to change it from frame-based data to sample-based data? That might be the right thing to do here.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by