2010-09-09 10 views
5

के 4 आउटपुट के रूप में अज्ञात फ़ंक्शन को परिभाषित करता है मेरे पास 4 आउटपुट के साथ एक एम-फ़ाइल फ़ंक्शन है। मैं एक अज्ञात फ़ंक्शन को परिभाषित करना चाहता हूं जिसमें एक ही इनपुट है, लेकिन केवल चार आउटपुट में से 2 उत्पन्न करता है। क्या यह संभव है?एम फ़ाइल फ़ंक्शन

उत्तर

3

यदि दो आउटपुट # 1 और # 2 हैं, तो सब कुछ ठीक है, और आपको अन्य दो आउटपुट के बारे में चिंता करने की आवश्यकता नहीं है।

दो outputs किसी भी दो अन्य हैं, तो आपके पास दो विकल्प

(1) बनाएं दो outputs (के साथ एक आवरण समारोह ध्यान दें कि Matlab के नए संस्करण में आप ~ द्वारा अप्रयुक्त आउटपुट dummy बदल सकते हैं।

है
function [out1,out2] = wrapperFunction(in1,in2,in3) 
    [dummy,out1,dummy,out2] = mainFunction(in1,in2,in3); 

(2) अन्य इनपुट चर है कि आप अपने समारोह के व्यवहार स्विच करने देता है जोड़े

function varargout = mainFunction(in1,in2,in3,outputSwitch) 
    %# make output switch optional 
    if nargin < 4 || isempty(outputSwitch) 
     outputSwitch = 0; 
    end 

    %# calculation here that creates out1-4 

    if outputSwitch 
     %# the special case where we only want outputs 2 and 4 
     varargout = {out2,out4}; 
    else 
     %# return all four outputs 
     varargout = {out1,out2,out3,out4} 
    end 

फिर आप सामान्य रूप से अनाम कार्य बना सकते हैं।

4

AFAIK, आप इसे केवल एक इनलाइन अज्ञात फ़ंक्शन के साथ नहीं कर सकते हैं, क्योंकि मैटलैब सिंटैक्स एक अभिव्यक्ति में किसी फ़ंक्शन के एकाधिक आउटपुट में कैप्चरिंग और अनुक्रमण का तरीका प्रदान नहीं करता है। लेकिन आप ऐसा करने वाले दो पुन: प्रयोज्य सहायक कार्यों को लिख सकते हैं, और फिर उनका उपयोग करके अज्ञात कार्यों को परिभाषित कर सकते हैं।

मान लें कि आपके चार बहस फ़ंक्शन को "f4" नाम दिया गया है।

function varargout = f4(x) 
%F4 Dummy function that returns 4 argouts holding identifying indexes 
varargout = num2cell(1:4); 

यहाँ एक पुन: प्रयोज्य सहायक समारोह है कि एक समारोह कॉल के आउटपुट remaps है।

function varargout = callandmap(fcn, ix, varargin) 
%CALLANDMAP Call a function and rearrange its output arguments 

tmp = cell(1,max(ix));  % Capture up to the last argout used 
[tmp{:}] = fcn(varargin{:}); % Call the original function 
varargout = tmp(ix);   % Remap the outputs 

अब आप इस तरह अज्ञात, बहस-रीमेपिंग फ़ंक्शन बना सकते हैं। यहां, जी में एक अनाम फ़ंक्शन है जो आपके मूल फ़ंक्शन के समान इनपुट लेता है, लेकिन इसके मूल 4 आउटपुट में से 2 लौटाता है।

>> g = @(varargin) callandmap(@f4, [2 4], varargin{:}) 
g = 
    @(varargin)callandmap(@f4,[2,4],varargin{:}) 
>> [a,b] = g('dummy') % gets argouts 2 and 4 from original f4() function 
a = 
    2 
b = 
    4 
>> 

वैरगाइन का उपयोग करके आप परिणामस्वरूप फ़ंक्शन हैंडल कहलाते समय पीछे के तर्कों को छोड़ने की अनुमति देते हैं। यदि आप जानते हैं कि सभी तर्क हमेशा प्रदान किए जाएंगे, तो आप चाहने पर पठनीयता के लिए नामांकित Argins का उपयोग कर सकते हैं।

आप यहां तक ​​कि प्रशंसक भी प्राप्त कर सकते हैं और इसे बंद कर सकते हैं।

function fcn = mapargout(fcnIn, ixArgout) 
%MAPARGOUT Create wrapper function that selects or reorders argouts 
% 
% fcn = argoutselector(fcnIn, ixArgout) 
% 
% Wraps a given function handle in a function that rearranges its argouts. 
% This uses closures so it may have performance impacts. 
% 
% FcnIn is a function handle to wrap. 
% 
% IxArgout is a list of indexes in to the original functions argout list 
% that should be used as the outputs of the new function. 
% 
% Returns a function handle to a new function. 

fcn = @extractor; 

    function varargout = extractor(varargin) 
    n = max(ixArgout); 
    tmp = cell(1,n); 
    % Call the wrapped function, capturing all the original argouts 
    [tmp{:}] = fcnIn(varargin{:}); 
    % And then select the ones you want 
    varargout = tmp(ixArgout); 
    end 

end 

यह अज्ञात फ़ंक्शन बनाने के लिए सरल कोड में परिणाम देता है। और आप इसे अन्य फ़ंक्शन रैपर कॉल के साथ लिख सकते हैं।

>> g = mapargout(@f4, [2 4]) 
g = 
    @mapargout/extractor 
>> [a,b] = g('dummy') 
a = 
    2 
b = 
    4 
>> 

लेकिन बंद मैटलैब में साथ काम करने के मुश्किल हो सकता है और प्रदर्शन प्रभाव हो सकता है। कॉलंडमैप दृष्टिकोण शायद तब तक बेहतर है जब तक आपको अतिरिक्त शक्ति की आवश्यकता न हो।

संबंधित मुद्दे