I have a function which takes 11 pairs of input arguments (each pair representing a type of process and an input commodity) and calculates a result based on all of those 22 values. However, I would like to make the function flexible such that if I wanted to use, for example, 3 pairs of input arguments rather than 11, it would still come up with a result and meanwhile ignore the other 8 pairs of unused arguments. Can you please tell me the simplest way to do that?
I've tried using varargin but when I pass fewer than 22 values as the input arguments, MATLAB tells me that the 'the (varargin) index (in the function file) exceeds the marix dimensions.'
Is there any way to pass 'inactive' input arguments so that MATLAB simply doesn't perform any operation on them?
Thanks.
Here's a sample of my code in the function file:
function [ Eprout,AnnEprout,Demand,AnnDemand,Status,AnnStatus ] = supdem(time, country, weights,supdataset,output, demdataset,... process1,process2,process3,process4,process5,process6,process7,process8,process9,process10,process11,... ipcom1,ipcom2,ipcom3,ipcom4,ipcom5,ipcom6,ipcom7,ipcom8,ipcom9,ipcom10,ipcom11)
Eprout = (sum(sum(supdataset(time,country,process1,ipcom1,output)))+sum(sum(supdataset(time,country, process2,ipcom2,output)))+ ... sum(sum(supdataset(time,country,process3,ipcom3,output)))+sum(sum(supdataset(time,country, process4,ipcom4,output)))+ ... sum(sum(supdataset(time,country,process5,ipcom5,output)))+sum(sum(supdataset(time,country, process6,ipcom6,output)))+... sum(sum(supdataset(time,country,process7,ipcom7,output)))+sum(sum(supdataset(time,country, process8,ipcom8,output)))+ ... sum(sum(supdataset(time,country,process9,ipcom9,output)))+sum(sum(supdataset(time,country, process10,ipcom10,output)))+... sum(sum(supdataset(time,country,process11,ipcom11,output))))/1000;
There are a number of ways of doing this. A simple alternative to varargin is to pass a logical vector with 0/1 values. In your case, the vector would contain 11 entries. The value of each entry would signal to the function whether to expect that variable pair in a separate array. Within the function you could have a series of if statements to check whether an entry in v is 1 or 0, for instance:
function [myoutputs] = myfunction(array_1,array_2,...., v);
default_1 = some_values1; % change some_values1 to whatever should be defaults for variable 2
default_2 = some_values2; % change some_values2 to whatever should be defaults for variable 2
% ... additional default initializations
if v(1)
pair_1 = array_1;
else
pair_1 = default_1;
end
% ... if statements for remaining pairs...
% ... use pair_1 pair_2 ...
For instance, passing the vector
v = [1 zeros(1,9) 1];
would indicate to the function that only the first and last parameter pairs are to be used.
You should still pass 11 pairs, but the content of the pairs you do not want to use can be empty or some other value.
Related
I have a set of functions that takes a cell array of tables and plots data from each table as a subplot on a figure. I have been learning to use the inputParser class to handle varargin inputs, and have that here. The optional parameters are for choosing plot type (plot or bar) and for choosing the names of the variables to plot from the input tables.
In my scenario, the inputs need to be passed along about 3 functions deep, so I'm wondering about best practices for doing this. In my current setup, the outermost function (main) takes a varargin input and parses the inputs by assigning defaults and such. Then I'm wondering, when it comes time to pass these inputs to the next function, is it best to pass the parsedInputs data Results struct down the line, or is it better to have the next function also take a varargin argument and to repeat the parsing process again? I'm not sure what the best way to go about this is. My code is below. The main script for test purposes looks as follows:
% RUN TEST CASE
Tables{1} = table([1 2 3]' , [6 7 8]', 'VariableNames', {'diam', 'length'});
Tables{2} = table([1 2 6]' , [6 9 2]', 'VariableNames', {'diam', 'length'});
Tables{3} = table([3 9 11]', [7 4 1]', 'VariableNames', {'diam', 'length'});
main(Tables);
The main function takes a (required) cell array of tables (Tables) and variable argument parameters, such as 'xVariable', 'yVariable', 'plotType'.
function main(Tables, varargin)%PlotParams, DICTS)
% parse inputs
parsedInputs = parse_plot_inputs(Tables, varargin);
% create figure of subplots
figure;
make_subplots_from_tables(parsedInputs);
end
A parse_plot_inputs function takes care of the default value assignment, etc.:
function parsedInputs = parse_plot_inputs(Tables, vararginList)
% input parser function for this specific plotting case
p = inputParser;
addRequired(p, 'Tables', #iscell);
addParameter(p, 'xVariable', 'diam');
addParameter(p, 'yVariable', 'length');
addParameter(p, 'plotType', 'plot');
parse(p, Tables, vararginList{:});
parsedInputs = p;
end
make_subplots_from_tables then loops through the cell array of tables, and calls plot_special to plot each of them on its own subplot.
function make_subplots_from_tables(parsedInputs)
% unpack parsed inputs
Tables = parsedInputs.Results.Tables;
% plot each table as a subplot
numTables = length(Tables);
for i = 1:numTables
subplot(numTables, 1, i); hold on;
plot_special(Tables{i}, parsedInputs)
end
end
plot_special is the "base" function in this scenario that calls the MATLAB plot functions:
function plot_special(T, parsedInputs)
% unpack parsed inputs
xVariable = parsedInputs.Results.xVariable;
yVariable = parsedInputs.Results.yVariable;
plotType = parsedInputs.Results.plotType;
% plot single table on one plot
xVals = T.(xVariable);
yVals = T.(yVariable);
switch plotType
case 'plot'
plot(xVals, yVals, '-x');
case 'bar'
bar(xVals, yVals);
otherwise
error('invalid plot type');
end
end
I am unsure whether this is the best method for taking in arguments and for using them in subsequent functions. This method works, although I'm not sure that it's the best practice, nor the most flexible, for example, considering the scenario when I would like to use plot_special on its own, and would like to be able to pass it arguments for xVariable, yVariable, etc. if need be. Given that it is currently dependent on the parsedInputs list from the main function, that wouldn't be doable. However, I'm unsure what another way to define it would be. I considered having an if statement built in along with a varargin input argument that checks whether the varargin is an already-parsed struct, or if it's getting the variables directly and needs to call the parse_plot_inputs itself to get things working. Any advice would be great.
There is no single "best" method, it all depends on circumstances. What you are doing is fine if the functions that take an inputParser object are private sub-functions. If they are generic functions that should work independently, they should have their own argument parsing. One thing you could do, given that you want plot_special to be a stand-alone function, is as follows:
function main(Tables, varargin)
figure;
make_subplots_from_tables(Tables, varargin{:});
end
function make_subplots_from_tables(Tables, varargin)
% plot each table as a subplot
numTables = length(Tables);
for i = 1:numTables
subplot(numTables, 1, i); hold on;
plot_special(Tables{i}, varargin{:})
end
end
function plot_special(T, varargin)
% parse `varargin` to yield `xVariable`, `yVariable`, `plotType`
% plot single table on one plot
end
This way you do the parameter parsing only where necessary, although it is done once for each table plotted, rather than only once for the whole graph. This is probably a very minor issue though, since parameter parsing is not nearly as expensive as plotting.
Is there something inbuilt in MATLAB which functions similar to the SQL COALESCE function. I want that function to return the first 'existing' value from all the arguments.
For example,
clear A B; C=10; COALESCE(A,B,C)
should return value of C (because A and B are unassigned/don't exist).
I know it would be very easy to code, and I am just being lazy here. But, I would be surprised if MATLAB doesn't have a similar function.
As far as I know there is no built-in function for that. But you can easily write your own.
Note that it is not possible in Matlab to pass a variable that has not been defined prior of using it. Therefore your proposed call clear A B; C=10; COALESCE(A,B,C) is invalid and will throw an error. Instead we can define an empty variable using var=[].
The following code creates two empty variables A, B and and assigns C=10. Inside the function coalesce we assume at the beginning that all variables are empty. In the for-loop we return the first non-empty variable. In the version without for-loop we get the index of the first non-zero element and then return the corresponding content of the cell if a non-zero element exists.
If you want the function to be accessible from everywhere within Matlab, see the documentation here.
function testcoalesce
A = [];
B = [];
C = 10;
COALESCE(A,B)
COALESCE(A,B,C)
end
% with for-loop (much faster)
function out = COALESCE(varargin)
out = [];
for i = 1:length(varargin)
if ~isempty(varargin{i})
out = varargin{i};
return;
end
end
end
% without for-loop (slower)
function out = COALESCE(varargin)
out = [];
ind = find(cellfun('isempty', varargin)==0, 1);
if ~isempty(ind);
out = varargin{ind};
end
end
The output is as expected:
ans =
[]
ans =
10
Timing the two functions showed, that the first solution using the for-loop is approximately 48% faster than the function without loop.
(10 samples, 1'000'000 iterations, 3 variables & 20 variables)
I would like to have a function function y(x) that, when I call y(5.12) will create a variable y(5.12) (or somehow remember what y(5.12) is) that will be available for use after the function is done running. What is the easiest way to do something like that?
I would've made an array and just stored each value in its corresponding cell, but I will also have non-integer and negative values, so array wouldn't work :(
Andrew Janke is spot on. Use a containers.Map. containers.Map is what is known as an associative array. It's also known as a symbol table, map, or dictionary. The backbone behind an associative array is that you access the array by a key and you get an associated value accompanied with this key.
The best example would be an English dictionary where the key is the word you want to look up, and the value is the definition of that particular word. For example (without any embellishment), supposing that our dictionary was f, and I used the key rayryeng, a possible value could be:
f('rayryeng') --> he is awesome
Now in MATLAB, the list of possible key types are: 'char', 'double', 'single', 'int32', 'uint32', 'int64', or 'uint64'. The list of possible values are 'char', 'logical', 'double', 'single', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', or 'uint64'. Given your application, it sounds like you want the key to be a double and the value to be a char.
As such, initialize your containers.Map to recognize this key/value combination:
f = containers.Map('KeyType','double','ValueType','char')
We get this:
f =
Map with properties:
Count: 0
KeyType: double
ValueType: char
There are currently no key/value pairs in this dictionary, and the expected key is a double and the output value is a char. We can start adding things at will:
>> f(5.12) = 'hello';
>> f(-1.56) = 'Solarmew';
>> f(pi) = 'YES!';
I've added in 3 keys of 5.12, -1.56 and pi with different strings. Now, if you want to retrieve a value given a key, just give the right key:
>> x = f(-1.56)
x =
Solarmew
If you try and give a key that doesn't exist, MATLAB will give you an error:
>> y = f(0)
Error using containers.Map/subsref
The specified key is not present in this container.
If you want to check to see if a key exists in the dictionary, use the isKey method:
>> isKey(f, 0)
ans =
0
Some more methods for you. You can retrieve all keys in the dictionary currently with the keys method:
>> k = keys(f)
k =
[-1.5600] [3.1416] [5.1200]
k is a cell array where each element is a key in this dictionary. Similarly, if you want the values, use the values method:
>> v = values(f);
v =
'Solarmew' 'YES!' 'hello'
It should be noted that both keys and values doesn't guarantee any order. This means that the order in which you added keys and values doesn't necessarily mean that you will get this same order when you call keys or values. Lastly, if you want to remove a key/value pair from the dictionary, use the remove method:
>> remove(f, -1.56);
>> k = keys(f)
k =
[3.1416] [5.1200]
>> v = values(f)
v =
'YES!' 'hello'
As you can see, the key of -1.56 associated with Solarmew is now removed from the dictionary.
Hope this helps!
I will present my solution using a simple array and a persistent variable.
Consider that you have a function that computes the square of a number and you want to do some memoization.
In my code, my_hist is a 2 dimensional vector. The first dimension corresponds to the input and the second to the square of the input (i.e., the output).
function y = memo_sqr(x)
persistent my_hist;
if(isempty(my_hist))
my_hist = [0 0];
end
%Find if it is already present
idx = find(my_hist(:,1)==x);
if idx
disp('Found Entry in Table')
y = my_hist(idx,2)
return
end
y = x^2;
my_hist = [my_hist; x y];
pause(2); %Artifical Lag
end
I introduce some artifical lag of 2 seconds to simulate the case where the calculation is time-consuming.
Now, if I run the code for the first time asking for memo_sqr(x), it takes some odd 2 seconds to compute (which includes the artifical lag). However, if I ask for it again, the persistent look-up table gives me the answer right away.
>> tic; memo_sqr(2); toc;
Elapsed time is 2.056881 seconds.
>> tic; memo_sqr(2); toc;
Found Entry in Table
y =
4
Elapsed time is 0.000545 seconds.
This is obviously a very simplistic example but it highlights how persistent variables can be used. Individual steps of my snippet can be optimized to fit your needs.
Say I want to create an anonymous function from a m-file-function that returns two outputs. Is it possible to set up the anonymous function such that it only returns the second output from the m-file-function?
Example: ttest2 returns two outputs, t/f and a probability. If I want to use the t-test with cellfun, I might only be interested in collecting the probabilities, i.e. I'd like to write something like this
probabilities = cellfun(#(u,v)ttest2(u,v)%take only second output%,cellArray1,cellArray2)
There's no way I know of within the expression of the anonymous function to have it select which output to return from a function with multiple possible output arguments. However, you can return multiple outputs when you evaluate the anonymous function. Here's an example using the function MAX:
>> data = [1 3 2 5 4]; %# Sample data
>> fcn = #(x) max(x); %# An anonymous function with multiple possible outputs
>> [maxValue,maxIndex] = fcn(data) %# Get two outputs when evaluating fcn
maxValue =
5 %# The maximum value (output 1 from max)
maxIndex =
4 %# The index of the maximum value (output 2 from max)
Also, the best way to handle the specific example you give above is to actually just use the function handle #ttest2 as the input to CELLFUN, then get the multiple outputs from CELLFUN itself:
[junk,probabilities] = cellfun(#ttest2,cellArray1,cellArray2);
On newer versions of MATLAB, you can replace the variable junk with ~ to ignore the first output argument.
One way to do this is to define the function:
function varargout = getOutput(func,outputNo,varargin)
varargout = cell(max(outputNo),1);
[varargout{:}] = func(varargin{:});
varargout = varargout(outputNo);
end
and then getOutput(#ttest2,2,u,v) gives only the p-value.
To use it in a cellfun you would need to run:
probabilities = cellfun(#(u,v)getOutput(#ttest2,2,u,v)...
This eliminates the need to write a wrapper every time, but then you have to make sure this function is always in the path.
Say I want to create an anonymous function from a m-file-function that returns two outputs. Is it possible to set up the anonymous function such that it only returns the second output from the m-file-function?
Example: ttest2 returns two outputs, t/f and a probability. If I want to use the t-test with cellfun, I might only be interested in collecting the probabilities, i.e. I'd like to write something like this
probabilities = cellfun(#(u,v)ttest2(u,v)%take only second output%,cellArray1,cellArray2)
There's no way I know of within the expression of the anonymous function to have it select which output to return from a function with multiple possible output arguments. However, you can return multiple outputs when you evaluate the anonymous function. Here's an example using the function MAX:
>> data = [1 3 2 5 4]; %# Sample data
>> fcn = #(x) max(x); %# An anonymous function with multiple possible outputs
>> [maxValue,maxIndex] = fcn(data) %# Get two outputs when evaluating fcn
maxValue =
5 %# The maximum value (output 1 from max)
maxIndex =
4 %# The index of the maximum value (output 2 from max)
Also, the best way to handle the specific example you give above is to actually just use the function handle #ttest2 as the input to CELLFUN, then get the multiple outputs from CELLFUN itself:
[junk,probabilities] = cellfun(#ttest2,cellArray1,cellArray2);
On newer versions of MATLAB, you can replace the variable junk with ~ to ignore the first output argument.
One way to do this is to define the function:
function varargout = getOutput(func,outputNo,varargin)
varargout = cell(max(outputNo),1);
[varargout{:}] = func(varargin{:});
varargout = varargout(outputNo);
end
and then getOutput(#ttest2,2,u,v) gives only the p-value.
To use it in a cellfun you would need to run:
probabilities = cellfun(#(u,v)getOutput(#ttest2,2,u,v)...
This eliminates the need to write a wrapper every time, but then you have to make sure this function is always in the path.