varargin

6 Ansichten (letzte 30 Tage)
AP
AP am 15 Mai 2011
How can I check the input argument of a function to see if a specific string has been input? For example, I want to see if the user has input 'filename' in the function argument then the function reads the file name in the argument as well and writes the data into it otherwise doesn't write anything to any file.
myfunc(x, y, z, 'filename', 'mydata.txt') ---> writes to mydata.txt
myfunc(x, y, z) ---> does not write to any file
Actually, I have 10 possible cases and I want to be able to check which of those cases is in the input argument and based on that take the appropriate action like plot function which you can specify 'color', linewidth', 'linetype', and other plot properties arbitrarily.
Thanks, Ahmad

Akzeptierte Antwort

Daniel Shub
Daniel Shub am 15 Mai 2011
doc inputParser
With 10 possibilities, I think an inputParser object is the way to go. With only 1 or two possibilities, the inputParser might also be teh way to go.

Weitere Antworten (1)

the cyclist
the cyclist am 15 Mai 2011
Define the function with
function [output] = myfunc(x,y,z,varargin)
then you can use nargin() to count how many inputs there were, and also see what values are stored in varargin.
  3 Kommentare
James Tursa
James Tursa am 15 Mai 2011
Then do what the cyclist suggested with a slight variation:
function varargout = myfunc(varargin)
Then run a loop over varargin, testing each input individually, and act accordingly.
AP
AP am 15 Mai 2011
This is what I was thinking but is there any command so that I can search in the cell array of VARARGIN? I tried ismember and strfind but they didn't work. do you have any suggestion?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Argument Definitions 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