Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

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.

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

Hello,

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
NRL
geoff.hazel@nrl.navy.mil

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

--------------63F3374B53EE
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.');
end

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

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

% 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);
end
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.
end
if ~exist([varname, ext]) % See if file exists.
msg = ['Can''t find file: ', varname, ext];
error(msg);
end
eval(command); % Load the file
eval(['out(:,m) = ', varname,';']); % Append it to output matrix
eval(['clear ', varname]); % Save memory
end

--------------63F3374B53EE--





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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.