Let Matlab continue without waiting for a result - matlab

I have the following question: How do I tell Matlab that it should not wait for the results of a function? Is there a way other than threads?
My problem: I have a function A that is called by a Timer every few seconds. If a specific event is met, another function B is called inside function A. Function B opens a Batch File.
I want function A to go on without waiting for function B to end. Is there a way to easily do it?
I'm sorry if this question was already asked, but I couldn't find a satisfying answer. Please also excuse my bad english.
I would like to thank everyone who answers for their help.

In your function B, just call the batch file with a & at the end of the line.
For example:
!mybatch.bat &
This will run the file mybatch.bat in background mode and will return execution to Matlab immediately after the call.
or if you prefer the complete form:
[status, result] = system('mybatch.bat &')
But in this case it is a bit useless, since the system call mybatch in the background, the result variable is always empty and status is always 0 (whether a file mybatch.bat was found and executed or not)
edit: That is the quick trick in case it is only the batch file execution which is slowing down your program.
If you have more matlab instructions in function B and you really need function A to go on without waiting, you will have to set up a listener object with function B as a callback. Then in your function A, trigger the event (which will activate the listener and call function B).

Related

AppleScript pass expression into function to be re-evaluated repeatedly? (or: AppleScript handler with callback?)

I think the correct description for what I'm trying to do is be able to pass an expression or function/handler into another handler as a parameter/argument. Some code to be evaluated inside the receiving handler. Similar to Javascript callbacks, I think.
For example, something like this:
on waitFor(theConditionExpression)
timeout_start(5) -- start a 5 second timer
repeat until (theConditionExpression or timeout_isExpired())
delay 0.1
end repeat
return theConditionExpression
end waitFor
theConditionExpression should be some expression or function that evaluates to a boolean result.
not really relevant to the question, but just FYI, timeout_start(…) and timeout_isExpired() are two simple handlers I've written that do exactly what they say. (…start() doesn't return anything, …isExpired() returns a boolean).
Of course, typically if I pass in some boolean expression, it will evaluate that expression once, at the time I pass it in. But I want it to evaluate it every time it's referenced in the code inside the handler.
Some languages (not sure about AS) have some kind of eval() function that you can pass it some code as a string and it will execute that string as code. Theoretically that could solve this, but: (a) I don't know if AS has anything like that, but even if it does, (b) it's not desired for various reasons (performance, injection risks, etc.)
So I'm thinking something more like eg. JavaScript's ability to pass in a function (named or anonymous) as function parameter/argument that can be re-evaluated every iteration in a loop, etc. (eg. like the compareFn argument in JS's Array.sort(compareFn)).
Can AS do anything like this, and if so how?
Thanks!
I'm going to suggest (pro forma) that an AppleScript application with an on idle handler is generally a better solution for wait conditions than a repeat/delay loop. It's more efficient for the system, and doesn't freeze up the script. But that would involve reconceptualizing your script, and I'm not certain it would work in this case, given the way you formed the problem.
There's an old but good site called AppleScript Power Handlers that shows a bunch of nifty-neato tricks for sophisticated use of AppleScript handlers: passing handlers as values or parameters; creating Script Objects within handlers; making closures and constructors. I'm pretty sure the answer to your request is in there. aLikely you'll want to set up a bunch of handlers that serve as condition expressions, then pass them as parameters to the evaluating handler. Or maybe you'll want to set up a script object containing the condition handlers and call it as needed?
At any rate, see what you can do with it, and ask more specific questions if you run into problems.

Abort processing in matlab with interrupt-check

I have a function in matlab that contains a few sub functions. Currently abort processing is done by looking for a flag before every sub function, then assert. PS: This is not in a GUI.
But some sub functions take longer processing time, maybe due to a loop, or a time consuming function in the sub function. So it takes longer to abort processing sometimes.
Is there any way I can set a thread to be constantly (or at a fixed interval) looking for the flag to abort? Or do I need to go in the sub functions, look for the flag in each loop and inside the time consuming function?
Thank you!

Matlab: simulate command window input by a script

I need a way to execute a script in matlab from within another script. The tricky part is, that the first script is a while loop, which polls every 60 seconds for a new job. If a job is found (created by me), the main.m file should be called. Now, the problem is that I want to be able to modify main.m and all other functions while the while loop is already running. The loop looks something like this:
while run
- pause 60 seconds
- get jobs form text file on network drive
- run main(jobID)
- save result on network drive
end
I thought the eval command would do the trick. But it doesn't. I think Matlab compiles all functions that could be called during runtime as soon as I start my while loop. From then on I cant change anything without restarting the while loop. (I can change it but Matlab ignores all changes).
Is there a way around this problem? I thought there was a method of executing commands inside the command window, just as if a user had typed it. But I cant remember.
Amro's solution with rehash works perfectly. Matlab detects changes in m-files even when it's already running a script!
I think you may be able to do what you want with a timer object.
First create a main.m function to define and initialize your timer:
function main
% Create timer that calls the sub-function checkmfiles every 60 seconds
t = timer('TimerFcn', #checkmfiles, 'Period', 60, 'ExecutionMode', 'fixedRate');
start(t);
function checkmfiles(obj,events)
% Just a dummy function, you'll need to expand it to fit your needs
if exist(f1) == 2
disp('''f1.m'' found.');
eval('f1');
else
disp('No file found.');
end
The example above assumes that f1.m doesn't exist initially. You can run main.m and then create an arbitrary f1.m (example assume that the function takes no inputs) afterwards that will be run by the timer callback. This method has the added benefit that it doesn't tie up your Matlab session while the timer runs.

matlab gui becomes unresponsive or partially closes

I am currently working on a matlab gui and after some beginner's problems with the data handling I am quite satisfied with the result.
There is just one hiccup: whenever the program is done running, the gui becomes unresponsive and the buttons and text elements vanish, all I can see is the background.
I have scanned the functions thoroughly for close all; statements and such, but there is nothing there.
How do I return 'clean' to the gui so I can put in more data? Do I need to put the gui in a constant while loop?
best wishes
Chris
You can do the following:
Modify the attribute of your controls to be interruptible:
set(handles.figure, 'Interruptible','on');
Create a callback function based on pressing a determined key combination.
set(KeyPressFcn, #resume_fcn);
Create a callback function that solves the problem.
function resume_fcn()
if eventdata.Key = ...
exit;
end
end
However, the consistency of data may be lost. In case you don't want to return 'clean' to the gui, you can type:
delete(get(0,'Children'))

Simulink: Synchronizing and timing

in order to simulate some processes I have a problem with getting a predefined working order of my self modeled blocks.
How can I be sure, that for example Block A must be finished before Block B and C start working?
The problem is, that some Blocks shall work after some others and some shall not. I must admit I have not much experience with Simulink in order of doing time-depending things (althought basic knowledge of simulink is available).
For instance this scenario shall be realised:
A -> B, C -> D, E, F
The main thing is, that all blocks A-F have no logic correlation to each other, they all do several things. My aim is to make B and C start working, after A has finished. And D/E/F after B AND C have finished.
In this case, the word "parallel" was the wrong word, this does not have to be calculated really parallely. Just making sure, that this complies with a predifined steady order.
Edit:
My new idea is to use the matlab workspace als buffer, so my block A can push its results to workspace (by the "to workspace" block). But now I have to make sure, that block B and C may read the results (with "From workspace") of A AFTER A pushed its information to workspace...how to do this?
Edit2:
Here's a screenshot which should make some thinks clearer:
As the documentation of "Sorted order" refers, the set up seems to be ok (including the subsystems timing). But unfortunately problem still exists. The variable "simin" is loaded from workspace, before it was written :( As you see, the display shows "1", which it shouldn't do. In the very first run of the simulation I get an exception, that the variable "simin" does not exist.
It would be nice, if you can help me with my issue.
Greets, poeschlorn
So in your example, if you have Block A connected with the same wire to both B and C, when Block A is finished, Block B and C will work in parallel.
EDIT:
I am using the same blocks as you are, but it works for me. I think you are over complicating things. The way you are setting block priorities is no different than how Simulink runs the blocks without them. Below you can see my setup and the output on both binary displays.
The error you see on the first run is due to Simulink not creating the variable until the first time step is being executed. When Simulink builds the simulation it sees that the variable used as input from the workspace is not created.
If the connection between the block are not enough to set the order, you can use block priorities.
A tip to test the execution order is to add an "embedded Matlab block" with a disp command displaying the name of the block.
It's not really clear what you're asking. When you say that Block A must be finished, do you mean the Output function? The way simulation works in Simulink is that the blocks are run serially, so Block B and C would never run until Block A finished it's Output function.
I don't know of any obvious way of running blocks B and C in parallel currently in Simulink.