Anonymous function with variable number of input and output arguments

35 Ansichten (letzte 30 Tage)
I am trying to write an anonymous function to generate strings for plotting several variables in multiple figures.
I would like to find an approach that will allow me to implement something like
vtext=@(v) horzcat('X1,',v,'1,''.'',X2,',v,'2,''.'',X3,',v,'3,''.''');
makevtext=@(varargin) vtext(varargin{:});
[atext,btext,ctext]=makevtext('a','b','c');
to produce the strings:
atext = X1,a1,'.',X2,a2,'.',X3,a3,'.'
btext = X1,b1,'.',X2,b2,'.',X3,b3,'.'
ctext = X1,c1,'.',X2,c2,'.',X3,c3,'.'
which I will be using to generate multiple plots of a, b and c later on. I'm using an anonymous function because I'd like to be able to easily change which variables I use this on as I explore my data (e.g., if I instead want to see figures for a, d and e, or for vtext to also include X4 and a/d/e4, etc.), but without having to go through my code and change the arguments of something like 20 plots every time I do this. For context, 1,2,3,4,... are different kinds of distributions (Pareto, exponential, etc.), while a,b,c,d,e,... are histogram counts, PDF, CDF, exceedance, and a number of other derivative functions.
Right now, the above code works if I run atext=makevtext('a'), but [atext,btext]=makevtext('a','b') gives me a "Too many input arguments" error. If I replace line 2 above with (just deleting the "{:}"):
makevtext=@(varargin) vtext(varargin);
then [atext,btext]=makevtext('a','b') gives me a "Too many output arguments" error, but I can also get (incorrectly):
[atext] = makevtext('a')
atext =
1x5 cell array
'X1,' 'a' '1,'.',X2,' 'a' '2,'.''
[abtext] = makevtext('a','b')
atext =
1x7 cell array
'X1,' 'a' 'b' '1,'.',X2,' 'a' 'b' '2,'.''
I know it's supposed to be possible to use both varargin and have multiple outputs in anonymous functions, but can't find any examples using both. Is there actually any way to do this, or do I need to use a different approach entirely?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Jan. 2018
DC = @(C) deal(C{:});
makevtext = @(varargin) DC(cellfun(@(V) vtext(V), varargin, 'uniform',0 ));

Weitere Antworten (2)

Stephen23
Stephen23 am 12 Jan. 2018
Bearbeitet: Stephen23 am 12 Jan. 2018
"I need to use a different approach entirely?"
Yes.
Your basic approach is to magically access variable names or evaluate strings of fieldnames, which means that you will simply waste lots of time writing these kind of magical functions to simply try and access your data. Accessing data is such a trivial task, why waste time on this? Surely you want to be actually processing/analyzing/... your data, rather than writing buggy hack solutions just to try and have access to your data?
You should use more standard MATLAB ways of accessing data. This would be simpler, neater, and much more efficient than what you are attempting now. In particular using indexing. I would suggest that you would be much better off using non-scalar structures, in which case accessing the data structure would be trivial using indexing and a fieldname, e.g. for "context" 2:
data(2).histcount
data(2).PDF
data(2).CDF
...
Of course you can nest these, and easily access those fieldnames dynamically:
fld = 'histcount'
data(2).(fld)
" but without having to go through my code and change the arguments of something like 20 plots every time I do this"
I completely agree. That is why having lots of special, unique names is a poor way to write code because it makes accessing them tedious. Use non-scalar structures and your code will be much simpler, and would means that you could plot it without having to magically deal with lots of special names. I think you need to redesign your data.
  1 Kommentar
dandan
dandan am 18 Jan. 2018
It sounds like I need to learn more about data structures. This is helpful, thank you!

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 11 Jan. 2018
makevtext=@(varargin) deal(vtext(varargin{:}));
  1 Kommentar
Walter Roberson
Walter Roberson am 11 Jan. 2018
No, the problem is that vtext() is only designed to accept one argument but multiple arguments are being passed in.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by