[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problem with reading lists
From: |
Quentin Spencer |
Subject: |
Re: Problem with reading lists |
Date: |
Wed, 09 Mar 2005 15:08:42 -0600 |
User-agent: |
Mozilla Thunderbird 0.9 (X11/20041127) |
John W. Eaton wrote:
On 9-Mar-2005, Quentin Spencer <address@hidden> wrote:
| I think this patch may be all that is necessary. I've never used
| textread--does someone want to test it?
Yes, this might be all that is *needed*, but that function would also
be a lot better if it avoided the use of eval. I don't see why it is
necessasry to use eval to create return values with the names a1, a2,
..., aN, and then use eval again to assign the temporary variables to
varargout{1}, varargout{2}, ..., varargout{N}. It seems it should be
possbile to do that in one step, without eval.
Good point. I've gone back and removed all references to eval, and
updated the documentation and changelog in the following patch. I tested
it on the simple case given in the help text and it seems to work
properly. If there are no objections, I'll check the updated version
into octave-forge CVS.
-Quentin
--- textread.m.old 2003-09-12 09:22:42.000000000 -0500
+++ textread.m 2005-03-09 15:03:36.718716296 -0600
@@ -23,7 +23,7 @@
## If n is omitted or negative, the entire file will be read.
##
## %#c formats return a character array of width #
-## %s and %[ formats return a list of strings
+## %s and %[ formats return a cell array of strings
## All other formats (see fscanf) return a column vector of doubles
##
## Example: say the file 'info.txt' contains the following lines:
@@ -33,8 +33,8 @@
## Columns from this file could be read with the line:
## [Lname, Fname, x, a] = textread('info.txt', '%s %s %e age=%d');
## then,
-## nth(Lname,3) is 'Penguin'
-## nth(Fname,3) is 'Tux'
+## Lname{3} is 'Penguin'
+## Fname{3} is 'Tux'
## x(3) is 6
## a(3) is 10
##
@@ -72,6 +72,8 @@
## 2002-03-15 Paul Kienzle
## * %[ produces a string
## * additional documentation
+## 2005-03-09 Quentin Spencer
+## * Changed list formatted outputs to cell array
function [varargout] = textread(file, format, n);
@@ -122,16 +124,13 @@
cat = setstr(cat);
for i=1:length(cat)
if cat(i) == "s"
- eval(sprintf("a%d=list();",i));
- elseif cat(i) == "c"
- eval(sprintf("a%d="";", i));
+ varargout{i} = {};
else
- eval(sprintf("a%d=[];", i));
+ varargout{i} = [];
endif
endfor
-
-
+
## Step 3: Read the contents of the file one term at a time.
## Place the terms into temporary octave matrices
## called 'a1', 'a2', et cetera. Each pass through
@@ -167,11 +166,11 @@
format(idx(i):idx(i+1)-1),row,i);
endif
if (cat(i) == "s") # list of strings
- eval(sprintf("a%d = append(a%d, data);", i, i));
+ varargout{i} = {varargout{i}{:}, data};
elseif (cat(i) == "c" ) # matrix of characters
- eval(sprintf("a%d = [ a%d ; data ];", i, i));
+ varargout{i} = [varargout{i}; data];
else # matrix of scalars
- eval(sprintf("a%d(%d) = data;", i, row));
+ varargout{i}(row) = data;
endif
endfor
++row;
@@ -187,19 +186,9 @@
prefer_column_vectors = pcv;
fclose(fid);
end_unwind_protect
-
- ## Step 4: populate the return values with the contents of a1, a2, etc.
+
if nargout == 0
- ret = list();
- for i = 1:nFields
- eval( sprintf("ret(i) = a%d;",i) );
- endfor
- vr_val_cnt = 1; varargout{vr_val_cnt++} = ret;
- else
- vr_val_cnt = 1;
- for i = 1:nargout
- eval( sprintf ("varargout{vr_val_cnt++} = a%d;",i) );
- endfor
+ varargout = {varargout};
endif
endfunction
- Problem with reading lists, Marcus Vinicius Eiffle Duarte, 2005/03/08
- Problem with reading lists, John W. Eaton, 2005/03/08
- Problem with reading lists, John W. Eaton, 2005/03/08
- Re: Problem with reading lists, Quentin Spencer, 2005/03/09
- Re: Problem with reading lists, John W. Eaton, 2005/03/09
- Re: Problem with reading lists,
Quentin Spencer <=
- deprecated builtin variables (was Re: Problem with reading lists), Quentin Spencer, 2005/03/09
- deprecated builtin variables (was Re: Problem with reading lists), John W. Eaton, 2005/03/09
- Re: deprecated builtin variables (was Re: Problem with reading lists), Paul Kienzle, 2005/03/09
- Re: deprecated builtin variables (was Re: Problem with reading lists), John W. Eaton, 2005/03/09