The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Load Numbered Files.
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Geoff Hazel

Posts: 8
Registered: 12/7/04
Load Numbered Files.
Posted: Jul 25, 1996 11:48 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

This is a multi-part message in MIME format.

Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


I complained yesterday about it being hard to load a bunch of ASCII data
files. Here's the function I wrote to solve my immediate problem:
numbered files. Hope someone finds it useful.

Geoff Hazel

PS Thanks to those who responded to yesterday's gripe with suggestions.

Content-Type: text/plain; charset=us-ascii; name="loadnumf.m"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="loadnumf.m"

function out = loadnumf(pre, nums, post, ext)
%LOADNUMF Load numbered ASCII data files into a matrix.
% Loads single-column ASCII data files with numerals in their names
% into a matrix using 'load'. An arbitrary list of numbers can be
% specified with letters before and/or after, and a filename
% extension. NOTE: The filename extension should include a '.' if
% one is desired. The function should work if the filenames lack
% extensions ('-ascii' added to 'load' command). If 'nums' is a
% matrix, the files are a loaded in column order.
% I/O fomat is : out = loadnumf(pre, nums, post, ext)
% where:
% out = Resulting matrix with data files in columns.
% pre = String containing any letters preceding the numerals.
% nums = List of numbers corresponding to numbered files to load.
% post = String containing any letters following the numerals.
% ext = String containing filename extension (including '.').

% GGHazel 7/24/96

% Check args for sanity
if nargin ~= 4
error('I Expect 4 input aruments.');

if ~isstr(pre) | ~isstr(post) | ~isstr(ext)
error('Input args not strings.');

if isempty(nums)
error('Empty numbers argument.');

% Figure out number of files, and reshaps 'nums' if necessary
[m, n] = size(nums);
if n ~=1 & m ~=1
nums = reshape(nums,1,m*n);
n = n*m;

% Do the work
for m = 1:n
varname = [pre,int2str(nums(m)),post]; % name of 'load's output var.
command = ['load ', varname, ext]; % The 'load' command.
if isempty(ext)
command = [command, ' -ascii']; % Deal with extentionless names.
if ~exist([varname, ext]) % See if file exists.
msg = ['Can''t find file: ', varname, ext];
eval(command); % Load the file
eval(['out(:,m) = ', varname,';']); % Append it to output matrix
eval(['clear ', varname]); % Save memory


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.