image thumbnail


version 1.0.0 (8.23 KB) by Miguel Couceiro
Read variables from ASCII file and returns a two column cell array with the variable name (first col) and the variable value (second col)


Updated 24 Jul 2021

View License

Read arbitrary variables from a text file
Input parameters:
fileName (mandatory) = Name of the text file (including aboslute or relative path) from which the variables should be read
The file must contain variables declared as Matlab attributions, like:
variableName = variableValue
where "variableName" is the name of the variable to be set and "variableValue" the corresponding value, which can be of any type (single value or array, including cell array). No semicolumns should be used to terminate the statement.
A single statement can be spread across several lines, by using the three dots (...) matlab notation. Empty paragraphs are discarded. Comments can be preceeded by % or # character, and will be discarded.
Output parameters:
This function returns an Nx2 text cell array of strings. Each of the N lines of the cell array contains the data corresponding to a given variable.
For a given line of the cell array:
the first column contains the name of the variable (in string format);
the second column contains the value of the variable (in string format).
The output can be used to set variables in any point of a given code, by using, e.x., the following code
for i = 1:size(importedVariables,1)
eval(strcat(importedVariables{i,1}, ' = ' , importedVariables{i,2}, ';'));
An ASCII file is provided and can be used to test the code.

Cite As

Miguel Couceiro (2021). ReadVariablesFromFile (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2020b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!