Azzera filtri
Azzera filtri

Problem with MATLAB Function block in Simulink

15 visualizzazioni (ultimi 30 giorni)
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.
  1 Commento
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.

Accedi per commentare.

Risposte (2)

Sebastian il 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:

Kaustubha Govind
Kaustubha Govind il 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 Commento
Alex Brissette
Alex Brissette il 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.

Accedi per commentare.


Scopri di più su Simulink Functions in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by