Please take a minute to review and accept our Terms of Use.
Welcome to the PLECS User Forum, where you can ask questions and receive answers from other members of the community.

Many technical questions regarding PLECS are answered on the Technical Solutions page of our website. Tutorial videos, specific application examples, and pre-recorded webinars are available on our YouTube page. Please follow us on LinkedIn for the latest Plexim news.

PLECS standalone - access output of a script in Octave?

0 votes

When the PLECS 'simulate' command is used in a simulation script, he output is returned as follows:

 out = plecs('simulate',opts); 

Is it possible to access the out structure from the Octave console window? After the script is run, the Octave workspace is empty.

Some variations on this question are:

  1. The console knows the 'plecs' key word but can not access  the components (e.g. a scope) in the current schematic. 
  2. When a script is pasted into the Octave window, it does not run. Is there a keyword to run a script from the Octave workspace (e.g. after copying it to a file).
  3. How can one debug a simulation script interactively (note that 2. would allow that).
asked Mar 29, 2021 by mhx (12 points)

1 Answer

0 votes

You are able to print text and simulation variables to the Octave console window, but the variables are not accessible by name from the console. The console window is primarily intended for diagnostics and debugging, and can be used as a computational environment (enter formulas, see results). Further, you can make use of Octave functions manually, but not execute full scripts (this is the purpose of the Simulations Scripts window interface).

If you leave statements open-ended (code lines do not end with a semicolon) you can effectively print the result to the Console, which may provide one form of debugging. Otherwise, there is a builtin compiler that provides error checking such as for syntax issues.

Does this answer your question?

answered Mar 30, 2021 by Kris Eberle (1,305 points)
> Does this answer your question?

This is indeed an answer to my question, although an unexpectedly unsatisfactory one.


I agree with Kris's points about the intended use of the console window.  However, a few workarounds come to mind.

One workaround is to simply save the workspace at the end of the simulation script, and then reload it into the PLECS workspace.   At the end of the simulation script, you would run "save output.mat". Then in the console, you can run "load output.mat".  The only item to be aware of is the *.mat path is relative to the *.plecs file.  This may not match the current directory of the console window.  There's a simple example below.

Nevertheless, the "plecs()" commands cannot be called from within the Console Window.  If you want a more interactive style of script writing, then the recommended approach would be to use XML-RPC style commands.  Then you can debug things from a Python window.

Thanks Bryan!

The save/load command is a clever idea. Unfortunately, in case of an error in the script I find the save command is not executed and debugging / exploring what is wrong is again impossible.

What prompted my question is that I had no idea how to address traces in the output struct. The example I can find in the help assumes there is only one scope trace and therefore accesses it by index (column 1) instead of by name. I will get better at this in due time, at the moment I'm still familiarizing myself with the verbose command structure.

The XML-RPC idea appears to be ideal but I will be building my own console/debugger interface instead of leveraging off of something which already does 90% of what I need.

Thanks for your help,



It is buried in the Simulation Scripts chapter, but you can find the following in our Online Help and User Manual documentation:

<If any outports exist on the top level of the simulated model, the command returns a struct consisting of two fields, Time and Values. Time is a vector that either contains the simulation time for each simulation step or the values in the OutputTimes of the SolverOpts in the optStruct parameter. The rows of the array Values consist of the signal values at the outports. The order of the signals is determined by the port numbers.>




> The rows of the array Values consist of the signal values at the outports.

and from the documentation:

Output Blocks in a Top-Level Circuit
If an output block is placed in a top-level circuit a unique output port number is assigned to the block. In PLECS Blockset, this port number determines the position of the corresponding output terminal of the PLECS Circuit block in the Simulink model.

Where is an "output block" defined? I am using PLECS standalone and there is no Simulink. My diagram is quite complex. The circuit browser does not list any "outports". If I click on likely candidates the browser highlights the name of the terminal (?) not what it is called in the help.


Indeed this refers to placing Signal Outport blocks from the PLECS (Standalone) component library in your top-level schematic, not in the same way for use within a subsystem block (or to connect to Simulink since PLECS Blockset doesn't use this internal scripting concept). Have you seen the demo model included in PLECS called "Buck Converter with Parameter Sweep" (this is the model that the example script in the documentation is based on)? See the attached screenshot from that model showing how the Outport block is connected to a Probe block, such that any probed signals will be saved as part of the resultant simulation struct.

By the way, if you added another Outport to the top-level schematic it would extend the length of the simulation struct with new Value data (e.g., if Outport 1 was connected to a bus of width 2 and Outport 2 was connected to a bus of width 1 then Out.Values(1:2,:) is associated with the first Outport and Out.Values(3,:) is associated with the second Outport.

Let me know if this is clear.

> such that any probed signals will be saved as part of the resultant simulation struct.

This strikes me as a fundamental and straightforward concept to 'export' signals to the output struct of a script (that is, if I understand you correctly). I can use this to get exactly the data that I want into Octave (maybe even non-scope data like Displays?).


As long as a signal is sent to an Outport block on the top-level signal you can access it in the Octave script and process the data however you like.