fgetvars

版本 1.0.0.0 (4.7 KB) 作者: Michael Yoshpe
Read double and string vector variables from a string or text file
97.0 次下载
更新时间 2015/7/6

查看许可证

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)

引用格式

Michael Yoshpe (2024). fgetvars (https://www.mathworks.com/matlabcentral/fileexchange/51969-fgetvars), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2012a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0

July 06, 2015 - added the ability to enclose vector in optional square brackets, and separate values by optional commas.
July 06, 2015 - added the ability to enclose vector in optional square brackets, and separate values by optional commas.