Using varargin when passing argument into a function in Matlab? - matlab

I have a function called viewcsi(varargin) and I want to pass in three variables at most.
The first is a MBSspectrum class I made and then a string and also a number.
viewcsi is a call back, it gets called like this:
...'ButtonDownFcn','viewcsi(''pickvox_cb'', sp_viewcsi)');
sp_viewcsi is the MBSspectrum class I made and is in the workspace. I want to be able to add another argument called counter which is integer of type double.
I want to do something like this:
...'ButtonDownFcn','viewcsi(''pickvox_cb'', sp_viewcsi, counter)');
or
...'ButtonDownFcn', {#viewcsi, 'pickvox_cb', 'sp_viewcsi', counter)');
But when I do the last two thing these do not work since they do not preserve 'sp_viewcsi' as a class but treats it like a string. What can I do to fix this? I have a feeling its something easy but I havent been able to figure it out.

The ButtonDownFcn will only ever pass it two arguments. You can cheat it by saying
...'ButtonDownFcn',#(a,b)viewcsi(a,b, counter));
so that the callback will pass it a and b, while Matlab will hand it the current value of counter.
See also the doc on passing extra parameters.

Related

Is it possible to use fsolve if an existing script return a class structure?

I have a script Function.m such that for example, when I write TEST=Function(1,2), I have TEST.x1=4 and TEST.x2=[5,6,7]. I want to use fsolve to help me find input. To be precise, I want to define a function, say a=#(y)Function(1,y)-4 so that when I use [z,vector]=fsolve(#(y)a(y),5), matlab can help me to obtain z=2 and vector=[5,6,7].
I would like to solve it by defining the same structure New_Function.m as Function.m such that it returns x1 values, i.e., TEST=New_Function(1,2) gives TEST=4 only. Then I write new_a=#(y)New_Function(1,y)-4 and solve z=fsolve(#(y)new_a(y),5) and define new_vector=Function(1,z) so that I can access new_vector.x2.
I want to know if it is possible to do my task without defining a new script or amending the content in the existing script. How to write code?
Since Matlab does not allow further referencing the result of a function call, you may need to help yourself with getfield. In your example (provided I got it right), it would be something like New_Func = #(y) getfield(Function(1,y),'x1'). This would take one scalar and return one scalar, i.e., New_Func(y) gives the field value of the struct returned by Function(1,y) associated to the field x1.

Tell IPython to use an object's `__str__` instead of `__repr__` for output

By default, when IPython displays an object, it seems to use __repr__.
__repr__ is supposed to produce a unique string which could be used to reconstruct an object, given the right environment.
This is distinct from __str__, which supposed to produce human-readable output.
Now suppose we've written a particular class and we'd like IPython to produce human readable output by default (i.e. without explicitly calling print or __str__).
We don't want to fudge it by making our class's __repr__ do __str__'s job.
That would be breaking the rules.
Is there a way to tell IPython to invoke __str__ by default for a particular class?
This is certainly possible; you just need implement the instance method _repr_pretty_(self). This is described in the documentation for IPython.lib.pretty. Its implementation could look something like this:
class MyObject:
def _repr_pretty_(self, p, cycle):
p.text(str(self) if not cycle else '...')
The p parameter is an instance of IPython.lib.pretty.PrettyPrinter, whose methods you should use to output the text representation of the object you're formatting. Usually you will use p.text(text) which just adds the given text verbatim to the formatted representation, but you can do things like starting and ending groups if your class represents a collection.
The cycle parameter is a boolean that indicates whether a reference cycle is detected - that is, whether you're trying to format the object twice in the same call stack (which leads to an infinite loop). It may or may not be necessary to consider it depending on what kind of object you're using, but it doesn't hurt.
As a bonus, if you want to do this for a class whose code you don't have access to (or, more accurately, don't want to) modify, or if you just want to make a temporary change for testing, you can use the IPython display formatter's for_type method, as shown in this example of customizing int display. In your case, you would use
get_ipython().display_formatter.formatters['text/plain'].for_type(
MyObject,
lambda obj, p, cycle: p.text(str(obj) if not cycle else '...')
)
with MyObject of course representing the type you want to customize the printing of. Note that the lambda function carries the same signature as _repr_pretty_, and works the same way.

Flowgorithm: importing variables and modifying their values

My professor likes work to be turned in in Flowgorithm.
I can't figure out how to take the variable X=0 from the main method to the displayLoop method and then allow the displayLoop method to modify the stored value so it can be used as a countdown (or countup in this case).
Flowgorithm, like Java, uses pass-by-value for types like Internet, Real, etc...
To return one less, you can create a function and return the decremented value. So, maybe try something like this:
count = displayLoop(count)
The assignment sounds a tad strange. Is this a recursion assignment?

Picking out the fourth value of a function using an anonymous function [duplicate]

I have a function that returns two values, like so:
[a b] = myfunc(x)
Is there a way to get the second return value without using a temporary variable, and without altering the function?
What I'm looking for is something like this:
abs(secondreturnvalue(myfunc(x)))
not that i know of. subsref doesn't seem to work in this case, possibly because the second variable isn't even returned from the function.
since matlab 2009b it is possible to use the notation
[~, b] = function(x)
if you don't need the first argument, but this still uses a temporary variable for b.
Unless there is some pressing need to do this, I would probably advise against it. The clarity of your code will suffer. Storing the outputs in temporary variables and then passing these variables to another function will make your code cleaner, and the different ways you could do this are outlined here: How to elegantly ignore some return values of a MATLAB function?.
However, if you really want or need to do this, the only feasible way I can think of would be to create your own function secondreturnvalue. Here's a more general example called nth_output:
function value = nth_output(N,fcn,varargin)
[value{1:N}] = fcn(varargin{:});
value = value{N};
end
And you would call it by passing as inputs 1) the output argument number you want, 2) a function handle to myfunc, and 3) whatever input arguments you need to pass to myfunc:
abs(nth_output(2,#myfunc,x))

Arrayfun syntax and usage with class method

Here's a line of code that's giving me trouble.
arrayfun(#(x)container.nodelist(x).config(#a_func_handle,0),2:6);
Container is a class with one of its properties being an object array of nodes, and that array is called nodelist.
Each node has a function called config that is used to initialize it. Config expects one input, one of which is a handle to a function. The function handle I'm passing needs a constant passed along with it, which is represented by the 0.
In this case, I want to configure the nodes at positions 2 through 6 in nodelist with a specific function, so I thought to use arrayfun instead of a for loop.
Unfortunately, Matlab barfs with "too many inputs" for function config. What am I writing wrong? Is this example clear?
I figured it out. What I ended up doing was using nested anonymous functions, like so:
arrayfun(#(y)y.config(#(x)(configSlave(x,0))),exp.pico_list(2:6));
If I've understood correctly, config is a method of the objects contained within your nodelist array. In which case, in the usual MATLAB manner, the object on which you're invoking the method gets passed as the first argument. For example, you might need to write the config method like this:
function config(obj, fcnHandle, value)
obj.FunctionHandle = fcnHandle;
obj.Value = value;
end