Problem with MATLAB Function block in Simulink

9 views (last 30 days)
I'd like to export data generated in Simulink to the Matlab workspace by using a Matlab function that writes the data to a variable in the workspace. Not very efficient, but necessary for my purposes. The problem is that the value passed into the Matlab Fcn block is sometimes incorrect, even wildly inaccurate. I placed a display block in parallel with the Matlab Fcn block and, occasionally, the displayed value does not match the value inside the function code (observed while debugging). Any clues to why this is happening?
Alternatively, is there a way to use a variable inside a structure as the target of the Simulink To Workspace block? Simulink won't allow the "structure.field" syntax as the To Workspace variable name.
Thanks!
  1 Comment
RICHARD ALEXANDER CÓRDOVA HERRERA
Edited: RICHARD ALEXANDER CÓRDOVA HERRERA on 24 May 2020
Hi Alex! I am working in a project and i got this issue. I need to process the data in real time, but the data from the Matlab Function Block is sometimes diferent than the simulink. I put a scope too, and verify this. Did you solve this?
I can use a filter, but sometimes I have very large peaks and it does not work.
Richard

Sign in to comment.

Answers (2)

Sebastian
Sebastian on 2 Mar 2011
The "To Workspace" block is indeed not able to write directly into some structure. If you need the data as part of the structure after your simulation finished, you could use To Workspace blocks, write to individual variables and then use the model's StopFcn callback to post-process, move the data to your structure and clean up. If you need the data in your structure while the simulation runs, then the To Workspace block does not help as it buffers the data till the end of the simulation. In that case using a MATLAB Fcn block might be a valid way, I would prefer to use a simple MATLAB code S-Function though, because it offers more flexibility. The following link shows an example how this might look like: http://www.mathworks.com/support/solutions/en/data/1-1AY2H/index.html

Kaustubha Govind
Kaustubha Govind on 2 Mar 2011
Addressing the inconsistency between the values seen in the MATLAB Fcn block and the Display block: Could this be because of the order of execution of the two blocks? If the Display block is executed after the MATLAB Fcn block, then what you see on the Display block while debugging is probably the value from the previous time-step. You can verify this by one of two ways:
  1. Display and examine the Sorted Order.
  2. Use a From Workspace block to log the signal and at the end of the simulation, compare the values written by your MATLAB fcn block and the From Workspace block.
  1 Comment
Alex Brissette
Alex Brissette on 2 Mar 2011
I think I see what you're saying, and I don't think execution order is the problem. I also connected a scope and a To Workspace block to the signal, and they recorded the values I expected. The array generated by the function code was not at all consistent with the other measurements, which agreed with each other.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!