How to run GUI in my script - matlab

I am using CVAP clustering toolbox that is based on GUI. After loading my data, I am using Run Clustering and Run validation commands, respectively. Then, choosing error rate option from tool menu. I need to repeat this process 20-30 times. And,in each time I need to save and open result file, to look at clustering outputs.To avoid this manual process, Is there any way to run GUI in my script? Basically, I just need to "click" Run clustering and Run validation button then choose Error rate from tool menu in my script.

Here is a simple example of how to use a GUI from a script. Assuming you know about GUI's this should make sense. If not let me know.
First get a handle of the GUI
guihandle = guidata(GUINAME);
Then in your script you can press buttons (execute the button callback function) using this type of command:
GUINAME('callback_functionname',guihandle.callback_functionname,callback_inputs,guihandle);
This will run whatever the callback does. Just make sure that before this you have manipulated whatever inputs the button callback will need. You mentioned you'll need to choose an error rate option. Since I don't know your exact code it's hard to say exactly how to do this. But from a script you can set the tool menu value like this:
set(guihandle.tool_menu,'Value',value);
Maybe that isn't a helpful example but that's the idea. Let me know if this doesn't make sense.

Related

Starting AnyLogic experiment programmatically

I need to run a large number of experiments and would like to do so over night as to waste as little time as possible. I have some output that I can export using PrintWriter, but I need to be able to start the next experiment programmatically after the other.
So something like
After experiment:
Experiment63.start().run();
If a parameter variation experiment doesn't do what you need and you really need to run mulitple sensitivity analyses, try this:
Create a new Custom Experiment
Delete everything in the properties window
Use YourExperimentClass.main(new String[] {}) to start each experiment.
For example, lets say you have three sensitivity analyses to run:
SensitivityToHeatExperiment.main(new String[] {});
SensitivityToSpeedExperiment.main(new String[] {});
SensitivityToFrictionExperiment.main(new String[] {});
These calls bring up a window for each experiment. Since experiments don't start automatically, you'll need to add that logic if you don't want to click "run" a bunch of times! In each Experiment's Initial experiment setup section, put run();. This automatically starts the simulation for you.
I haven't quite figured out how to close the windows automatically using this approach: system.exit(0) and experiment.close() shut all windows opened by the experiment, so you'll need a way to tell if all experiments are done running. One option is to use a common file and a FileLock to ensure the simulations don't encounter concurrency problems. Note that FileLock might be handy if all sensitivity experiments need to write to common files.

step by step simulation in command line for Matlab Simulink model

I want a solution for command line implementation for Matlab Simulink. In simulink model there is simulation switch called "step forward". With this I am able to run step by step and able to see the outputs in scope for each sample time.
I am also able to change some inputs for constant input blocks at any sample period time. However, I want to do this in command line, because I have a huge model and I want to reuse this model verification script.
I am able to do some thing like this using "sim" command. But the sim command is just simulating the entire model at once, and I could not observe/change the intermediate outputs/inputs. The "sim" command with a certain stop time is running for certain sample periods, but later if I give the next "sim" command, it is running from the beginning and not starting from where it stopped. But, is there any exact Matlab command to step and pause (for one sample period time) the simulation, like that in simulink gui.
You want to use SimState to save the states of the model at the end of one simulation, and restore them at the beginning of the next simulation. How to do this is described in the documentation under Save and Restore Simulation State as SimState.
You cannot make any structural changes to the model (e.g. add or remove block) between each call to sim. There are also a few blocks which don't support SimState, see the documentation for more details. Note that this is for the latest release (R2015a), more and more blocks were added to support SimState over the past few years, so you want to check the release notes depending on the release you are using.
You can try this:
sldebug('gcs');
you are now in debug mode, you can type in help to get an overview of the available commands. To run through the model type in:
next
Each time you type next, you move the simulation one step forwards.
The following link will provide you with further details on how stepping through a simulation works.
http://www.mathworks.com/help/simulink/ug/how-stepping-through-a-simulation-works.html
For more details on using the debugger to run through the simulation step by step you can go to:
http://www.mathworks.com/help/simulink/ug/running-a-simulation-step-by-step.html

Managing multiple anylogic simulations within an experiment

We are developing an ABM under AnyLogic 7 and are at the point where we want to make multiple simulations from a single experiment. Different parameters are to be set for each simulation run so as to generate results for a small suite of standard scenarios.
We have an experiment that auto-starts without the need to press the "Run". Subsequent pressing of the Run does increment the experiment counter and reruns the model.
What we'd like is a way to have the auto-run, or single press of Run, launch a loop of simulations. Within that loop would be the programmatic adjustment of the variables linked to passed parameters.
EDIT- One wrinkle is that some parameters are strings. The Optimization or Parameter Variation experiments don't lend themselves to enumerating a set of strings to be be used across a set of simulation runs. You can set a string per parameter for all the simulation runs within one experiment.
We've used the help sample for "Running a Model from Outside Without Presentation Window", to add the auto-run capability to the initial experiment setup block of code. A method to wait for Run 0 to complete, then dispatch Run 1, 2, etc, is needed.
Pointers to tutorial models with such features, or to a snip of code for the experiment's java blocks are much appreciated.
maybe I don't understand your need but this certainly sounds like you'd want to use a "Parameter Variation" experiment. You can specify which parameters should be varied in which steps and running the experiment automatically starts as many simulation runs as needed, all without animation.
hope that helps
As you, I was confronted to this problem. My aim was to use parameter variation with a model and variation were on non numeric data, and I knew the number of runs to start.
Then i succeed in this task with the help of Custom Variation.
Firstly I build an experiment typed as 'multiple run', create my GUI (user was able to select the string values used in each run.
Then, I create a new java class which inherit from the previous 'multiple run' experiment,
In this class (called MyMultipleRunClass) was present:
- overload of the getMaximumIterations method from default experiment to provide to default anylogic callback the correct number of iteration, and idnex was also used to retrieve my parameter value from array,
- implementation of the static method start,
public static void start() {
prepareBeforeExperimentStart_xjal( MyMultipleRunClass.class);
MyMultipleRunClass ex = new MyMultipleRunClass();
ex.setCommandLuneArguments_xjal(null);
ex.setup(null);
}
Then the experiment to run is the 'empty' customExperiment, which automatically start the other Multiple run experiment thru the presented subclass.
Maybe it exists shortest path, but from my point of view anylogic is correctly used (no trick with non exposed interface) and it works as expected.

How to export simulink data to workspace during simulation?

I want to retrieve the data from simulink during simulation, and use serial network function to send these data to another program. Because I need to use another program to do some tricks and send command back to simulink, so I have to get data from simulink during runtime so that another program can make the right command.
I've tried using To Workspace block to export the data.
However, I can only got value in the very beginning of the simulation.
And I've also tried using scope and change some properties: check Save Data To Workspace and Uncheck Limite data to Last.
First, I started simulation, and I found the ScopeData didn't appear in the Workspace. Only when I stop simulation, ScopeData would appear in workspace.
And after that, I can use ScopeData.signals.values to get values.
But what I want is: when I start simulation, ScopeData would appear in workspace so that I can send these data to other program.
Does anyone know how to achieve this?
I found this page might be helpful, but I still don't know how to continuously export data during simulation.
Use get_param to read data from just at the current time. Also to send the data back to Simulink with set_param of a gain or another block.
An example of get_param
First load and start the simulation:
load_system('myModel')
set_param('myModel','SimulationCommand','Start');
To read data on any line of your simulink model:
Get a simulink block object (let's try a Clock with the name Clock):
block = 'myModel/Clock';
rto = get_param(block, 'RuntimeObject');
Then get the data on its first (or any) output port (or input) of that block.
time = rto.OutputPort(1).Data;
You could do the reading, in a timer callback.
Also this might be helpful: Command Line Functionality for Simulink
During simulation Simulink stores logged data in an internal buffer and only writes the data to the Workspace when the simulation is paused or stopped.
It sounds as if you really need to write an S-function (which will get signal values on a timestep-by-timestep basis) and communicate with Proteus that way.
Of course Simulink is a non-realtime simulator, so if you are talking about doing anything resembling real-time control then you are most likely taking the wrong approach altogether.
At any time during simulation you can force Simulink to write the simulation output data to the workspace:
set_param(bdroot,'SimulationCommand','WriteDataLogs');
I've found that this command is quite unstable in my Matlab 2010a for Win64. In particular I have to avoid it when simulation is stopped (i.e. first check
get_param(bdroot,'SimulationStatus') ), otherwise Matlab shows an error and asks to be restarted.

Avoid interruption of callback functions in Matlab GUI

I have a Matlab GUI that needs a high time to execute some callback functions. Besides, these functions include the following code:
drawnow('expose');
pause(handles.data.delay);
I want to avoid that those callback executions get interrupted in order to avoid data inconsistency if the user presses other buttons. Thus, I modify the figure settings as:
set(handles.figure, 'BusyAction','cancel', 'Interruptible','off');
However, the callbacks are still interrupted. How can I avoid it?
Note: I think that the problem is that I need to propagate the 'BusyAction' and 'Interruptible' values to all the controls in my GUI, is there any way to do it automatically? Like, for example, modifying the default value before generating the GUI.
The fastest and cleanest way to propagate any property to all UI objects is with findobj:
set(findobj('Type','uicontrol'), 'BusyAction','cancel', 'Interruptible','off');