I have a parameter {?Calendar Year} that takes on year values (2014,2015, etc.) that I would like to use in a formula. I would like it to function how one might think this would work:
{table.date}>CDate({?Calendar Year},01,01).
Does anyone know how I can accomplish this?
Related
I have a list of symbols, say
`A`B`C
. I have a table tab0; A function that takes in a table plus a string as arguments.
tab1: f[tab0;`A]
tab2: f[tab1;`B]
tab3: f[tab2;`C]
I only care about the final values. But my list of symbols can be long and can have variable length, so I don't want to hardcode above. How do I achieve it?
I think it has something to do with https://code.kx.com/q/ref/accumulators/ but I really struggle to figure out the syntax.
This is exactly the use case for the binary application of over (/) (https://code.kx.com/q/ref/accumulators/#binary-application)
So you should use:
f/[tab0;`A`B`C]
So, I want to make some calculations using the arguments I passed in the function.
My function looks like this function f=myFunc(arg1,arg2,arg3) and I can give 2 or more arguments in total.
I call it from the command windows as myFunc(2.0,3.4,1.6).
I want to calculate the product of those numbers which is easy if the number of arguments was constant and not dynamic.
Is there a way to get those arguments into a list to use it later as I please?
Something like that:
myList=function_args()
>>>[2.0,3.4,1.6]
so I can pass the list to the prod function prod(myList)
Is there an idiomatic way in Matlab to bind the value of an expression to the nth return value of another expression?
For example, say I want an array of indices corresponding to the maximum value of a number of vectors stored in a cell array. I can do that by
function I = max_index(varargin)
[~,I]=max(varargin{:});
cellfun(#max_index, my_data);
But this requires one to define a function (max_index) specific for each case one wants to select a particular return value in an expression. I can of course define a generic function that does what I want:
function y = nth_return(n,fun,varargin)
[vals{1:n}] = fun(varargin{:});
y = vals{n};
And call it like:
cellfun(#(x) nth_return(2,#max,x), my_data)
Adding such functions, however, makes code snippets less portable and harder to understand. Is there an idiomatic to achieve the same result without having to rely on the custom nth_return function?
This is as far as I know not possible in another way as with the solutions you mention. So just use the syntax:
[~,I]=max(var);
Or indeed create an extra function. But I would also suggest against this. Just write the extra line of code, in case you want to use the output in another function. I found two earlier questions on stackoverflow, which adress the same topic, and seem to confirm that this is not possible.
Skipping outputs with anonymous function in MATLAB
How to elegantly ignore some return values of a MATLAB function?
The reason why the ~ operator was added to MATLAB some versions ago was to prevent you from saving variables you do not need. If there would be a syntax like the one you are searching for, this would not have been necessary.
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))
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.