How to obtain the output of a timer callback function in Matlab - matlab

Suppose I have a function with the following input/output:
[out1,out2]=func(in1,in2);
and I want to call the function at a particular time using timer callback.
I understand the syntax to call func is
t=timer;
t.TimerFcn=#(~,~)func(in1,in2);
But how I do get the outputs of func?

According to the documentation page for Timer Callback Functions the example callback functions:
function myfile(obj, event)
function myfile
function myfile(obj, event, arg1, arg2)
Further reading of the page suggests that in general the proper function declaration looks something like:
function my_callback_fcn(obj, event, ...)
This, to me, is wrong evidence that there is no way to get a return value from the callback function.
I would suggest using the callback function to set an object property or a "global" variable with the desired return value(s).

Related

how to use string variable call the same name Function in Flutter?

I want to use string parameter to call the same name function
At one time i try call this method ,but also need function argument
Function().apply
so, I want know how to implementation this idea? thx

Matlab alter attribute value by method

I tried to change an attribute value of a class by invoking one of its member function:
p1 = tank();
p1.checkOri(p1);
And in the class definition, I have that static method:
classdef tank
properties
value
...
end
methods
...
methods (Static)
function obj = checkOri(obj)
if (CONDITION) %the thing I want it to do
obj.value = EXPRESSION;
...
Yet this checkOri method is not working. But if I write this method in the main file, or to say altering the value of p1-the instance of class tank-it works perfectly:
p1 = tank();
p1.checkOri(p1);
if (CONDITION) %the thing I want it to do
p1.value = EXPRESSION;
It works perfectly.
I wonder what caused this. From my experience with other programming languages, invoking method should have worked, is it because of some tricks with Matlab syntax or static method? How could I fix it so that this method would work?
So, as #Navan in the comment said, handle class could be a solution.
It appears Matlab has a similar parameter concept with Java and C++, arguments modified in a function/method only remains that modification inside the function/method.
For this class, I simply added < handle in the head of class definition and it worked:
classdef tank < handle
properties
...
But I am not sure if that is the only solution, there might be better ways to do this. So I'll leave that question open, you are more than welcomed to post your opinion:D
In MATLAB, the call
p1.checkOri();
is equivalent to
checkOri(p1);
In both cases, the class method checkOri is called for the class of object p1, passing p1 as first argument to the function by value.
Because p1 is passed by value, any changes made to it inside the function are not seen by the object in the calling workspace. Therefore, one typically does
p1 = checkOri(p1);
This way, the object that was passed by value and modified inside the function is passed back out and assigned to the variable that held the original object.
If the method is written as follows:
function obj = checkOri(obj)
%...
end
then MATLAB will optimize the above function call such that no copy of the object is actually made. Note that both in the function declaration and in the function call, the input and output variable is the same.
As already discovered by OP, the above does not hold for handle classes, classes that inherit from handle. These classes act as if they are always passed by reference, and any change made to them in any workspace will be reflected in all other copies in other workspaces.
Also assigning to a member variable does not follow the above, such that
p1.value = 0;
modifies object p1.
For more information on the difference between value classes and handle classes see this other question.

Sailsjs 0.12 - Custom method for all models

I am trying to add Laravel-like Mass Assign to my Sails 0.12 application.
So far I have done:
Add method secureUpdate to config\models.js:
console.log(this);
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
I am trying to get the calling model name, just like in seed method in this gist(for seeding data): https://gist.github.com/juanpasolano/5c7596d8629eeeb8debd#file-config-models-js
But the value of this is diffirent, with no adapter object(undefined)
I do not understand why the value of this is not the same. Could you help me out?
I write the answer here just for others who may have the same problem in the future :
The this is undefined because you are defining the method as an arrow function, you have to use a function expression, from MDN :
An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

why when we call the "task" variable, it has to be written as "task()"

I'm new to Swift and is trying to learn the concept of extension. I saw this code on youtube. My question is, why when we call the "task" variable, it has to be written as "task()". I understand if I delete the "()" from "task()", the code wouldn't work. But I am confused as to why "()" must be included when we tried to use the variabe "task".
extension Int {
func repetition(task: () -> Void){
for _ in 0..<self{
task()
}
}
}
3.repetition({print("hello")})
Variables can contain functions. When they're written like that they are called closures. There is a lot to read and learn about closures. When your variable contains a function and you want to call that function, you add the brackets and any parameters the function needs.
Have a look at the documentation on closures here.
In your case, the definition of task is () -> Void which means it's a function that takes no parameters and returns no result (void). When closures take parameters, you can refer to them by a name of your choosing, or positionally using $0, $1, etc.
Closures are often used in asynchronous functions to call back to your code when they've finished.

invoke method via returned ref from "can"

Normally, to call a method from its reference, it should go something like below:
$methodref->(#args)
But when invoke a method from its reference returned from "can", it seems inconsistent.
$methodref = $my_obj->can(my_method);
$my_obj->$methodref(#args) if $methodref; # why it isn't $my_obj->$methodref->(#args)?
Can someone please shed some light on this?
Thanks,
CC
The reason is that the object has to be the first thing passed in to the function. This is automatically done for you if you make a method call. But $methodref->($my_obj, #args) will also work.
The CODE ref returned by can doesn’t have an object associated with it once that can is done. That’s why you still need to use an object as an invocant.
Note that this is dangerous, because there is no longer any guarantee that this is method that is supposed to be called on that object. I suppose you might do something like this:
$objmethref = $my_obj->can("methname") && sub { $my_obj->methname(#_) };
# then later
$objmethref->(#args);
but I’m not sure what it is you really want to do.
$my_obj->$methodref->(#args)
is
( $my_obj->$methodref() )->(#args)
In other words, it would call the method with no args, and attempt to use the result as a function reference. It's not what you want at all
The obvious means of calling it is
$methodref->($my_obj, #args)
but Perl provides a syntax that looks like a method-call for your pleasure.
$my_obj->$methodref(#args)
$methodref->(#args) is a function call, not a method call, so won't have the object automatically passed.
$my_obj->$methodref->(#args) would call $methodref with no parameters other than the object parameter, and use its return value as a coderef, calling that and passing it the indicated args.