octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: new function: textscan.m


From: Ben Abbott
Subject: Re: new function: textscan.m
Date: Sat, 23 Oct 2010 13:34:32 +0800

On Oct 23, 2010, at 10:14 AM, Liam Groener wrote:

> On Oct 22, 2010, at 6:46 PM, Ben Abbott wrote:
> 
>> On Oct 23, 2010, at 8:53 AM, John W. Eaton wrote:
>> 
>>> On 23-Oct-2010, Ben Abbott wrote:
>>> 
>>> | I've made an attempt to implement the missing function textscan.m
>>> | 
>>> | If there are no suggestions for improvement, I'll commit.
>>> 
>>> +  if (nargin > 2 && isnumeric (varargin{1}))
>>> +    N = varargin{1};
>>> 
>>> I think it would help to quickly understand what N is if you used
>>> nlines or similar instead of N.  Also, we generally try to avoid
>>> uppercase variable names in Octave.
>>> 
>>> +  if ((! strcmp (class (fid), "double") || fid < 0) && ! ischar (fid))
>>> +    error ("textscan: first input argument must be a valid file id, or 
>>> string.");
>>> +  endif
>>> +
>>> +  if (! ischar (formatstr) && ! isempty (formatstr))
>>> +    error ("textscan: second input must be a format specification.");
>>> +  endif
>>> 
>>> Maybe I'm just slow, but I have a harder time understanding negative
>>> conditions like the ones above.  Instead of checking the conditions
>>> that lead to errors, I find it simpler to write and easier to
>>> understand code later if I test the conditions for success instead.
>>> For example, instead of the above, I would write something like
>>> 
>>> if (isa (fid, "double") && fid > 0 || ischar (fid))
>>>  if (ischar (formatstr) || isempty (formatstr))
>>>    ## ... code to do the real work here ...
>>>  else
>>>    error ("textscan: second input must be a format specification");
>>>  endif
>>> endif
>>> else
>>>  error ("textscan: expecting first argument to be a file id or character 
>>> string");
>>> endif
>>> 
>>> Is that condition on formatstr correct?  Is it OK for it to be empty
>>> if it is not a character string?
>>> 
>>> Note also that isa is probably better than class+strcmp.  But what
>>> happens if fid is a matrix?  Should we check for that?  Should we
>>> maybe have a is_valid_file_id function?  Maybe that would also be
>>> useful in other places too.
>>> 
>>> jwe
>> 
> Hi Ben,
> 
> I thought that, in Matlab, N is the number of times that the format string is 
> repeated (as in textread), not the number of lines to be read. Did you intend 
> to make this change? (Or am I all wet?)
> Liam

I have never used texscan before this week. It would be wise to be skeptical of 
my understanding for how Matlab's version works.

Can you provide me an example that illustrates the difference between repeating 
for format string, and reading the number of lines?

Ben



reply via email to

[Prev in Thread] Current Thread [Next in Thread]