fgetvars
This package includes the following files:
sgetvar2.m - reads a single double or string variable from an input string
sgetvars.m - reads multiple double or string variables from an input string
fgetvars.m - reads multiple double or string variables from an input text file
test.inp - input file example
To test fgetvars function, save all the files to the same directory and execute:
[dvar1, svar1, dvar2, svar2]=fgetvars('test.inp', 'dvar1', 'svar1', 'dvar2', 'svar2');
The following is a help section of fgetvars function:
%fgetvars read double and string variables from a text file
% Input :
% InpFN - input file name
% varargin - variable names list (strings, case sensitive)
% Output :
% varargout - output variables list
% Output variables can be double (scalar or vector), or string (single
% or multiple). When the variable contains multiple strings, a charachter
% array of size (n_str_vals x max_str_len) is returned, where n_str_vals
% is the number of string values, and max_str_len is the maximum string
% length (all shorter string will be padded with blanks at the end).
% The values for var_name should come after '=' separator, and are read
% until either the end of line, or ';' separator. The values are
% separated by white spaces or optional commas.
% Once a non-white charachter is encountered, the input is immediately
% terminated, and the variable is assigned the values that were
% successfully read up to this moment.
% Vectors can be enclosed in optional square brackets, similar to regular
% Matlab vector notation.
% Double variables can be scalars or vectors, and can include numbers in
% scientific notation.
% String variables can have multiple string enclosed in quotation marks,
% as long as they are on the same line. When more than one variable names
% are found in the same input string, only the last one is read.
% Usage example:
% The following is an example of an input file test.inp (remove the
% first '%' charachter to recreate the actual file)
%
% % line containing only comments ('%' character is optional)
% dvar1 = l0.0 double scalar ('%' character for comments is optional)
% dvar2 = 1.5 2.5 3 ; % vector (';' separator is optional)
% svar1 = 'one string' % single string
% svar2 = 'first string' 'second string' % 2 strings
% line containing only comments (no '%' character)
% dvar1 = 5.0 % should override first dvarl
% dvar2 = -1 0 1 2.5 % second dvar2 variable, should override first one
%
% [dvar1, svar1, dvar2, svar2]=readvars('test.inp', 'dvar1', 'svar1', 'dvar2', 'svar2');
% will read variables dvar2, svar1, dvar2, svar2 from input file test.inp
% Remarks:
% Revision history:
% July 06, 2015 - added the ability to enclose vector in optional square
% brackets, and separate values by optional commas.
% July 03, 2015 - created (Michael Yoshpe)
Cite As
Michael Yoshpe (2024). fgetvars (https://www.mathworks.com/matlabcentral/fileexchange/51969-fgetvars), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0.0 |
July 06, 2015 - added the ability to enclose vector in optional square brackets, and separate values by optional commas.
|