Can I make readtable to be of single precision for numeric values?

6 views (last 30 days)
Hello everybody,
I am running an extensive for-loop inside a while-loop that at each respective iteration optimizes a different problem and pass information between each optimization. Given that I want to make it faster and consume less memory, I have been trying different approaches and want the optimization process to use single-precision variables as much as possible.
However, an important chunk of variables are created when the code reads information from external files using readtable, and I want to make sure that the numeric values or numeric variables are of single precision.
Have not been able to find some information about it in the documentation. So, my question is: ¿is there a way for me to make numeric variables/numeric input of readtable to be of single precison?
Thanks everybody for their time.

Accepted Answer

Stephen23
Stephen23 on 2 Jan 2023
Edited: Stephen23 on 2 Jan 2023
"¿is there a way for me to make numeric variables/numeric input of readtable to be of single precison?"
Two approaches to specify the class of numeric data when importing numeric data:
  1. specify the FORMAT option (using TEXTSCAN() formats)
  2. use SETVARTYPE() before calling READTABLE().
Method 1: FORMAT option:
The READTABLE documentation explains the FORMAT option in the text file options section:
The format that used by TEXTSCAN():
fnm = 'test.txt';
tb1 = readtable(fnm, 'Format','%f32%f%f') % first column SINGLE
tb1 = 4×3 table
X Y Z __ __ __ 11 12 13 21 22 23 31 32 33 41 42 43
class(tb1.X)
ans = 'single'
Method 2: SETVARTYPE() function:
The READTABLE() documentation describes the import options object here:
The supporting functions are:
obj = detectImportOptions(fnm);
obj = setvartype(obj,'X','single'); % first column SINGLE
tb2 = readtable(fnm, obj)
tb2 = 4×3 table
X Y Z __ __ __ 11 12 13 21 22 23 31 32 33 41 42 43
class(tb2.X)
ans = 'single'
  1 Comment
Kenny Giron
Kenny Giron on 3 Jan 2023
Thank you. Now I understand how to browse deeper in the documentation. I need to dig deeper in the supporting functions and the additional links shown in the main function documentation.
I think the SetVarType is so much more intuitive though. Thanks for showing me that.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!