keeping the variable names same

2 views (last 30 days)
i am importing a text file and keeping the variable header names same by using:
readtable('111..txt','PreserveVariableNames',true)
what is the function of true in this code?

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jan 2022
MATLAB uses Name/Value pairs in which the first element of the pair tells the option name and the second element of the pair indicates the state to use.
MATLAB does not use option keywords (that force behaviour if they appear by themselves). Keyword options are harder to parse. If you use name/value pairs consistently then each routine can parse for the options it is interested in and can ignore the other options; but if you permit keyword options that take effect just by appearing alone, then every parsing routine that deals with those options would have to know the complete list of keyword options so it would know whether to advance by one or two positions to keep looking for more options.
So, 'PreserveVariableNames', true means that names are to be preserved, and 'PreserveVariableNames', false would mean that names are not to be preserved (which is currently the default.)
  2 Comments
Walter Roberson
Walter Roberson on 25 Jan 2022
You must pass each option that you want to have different than the default.
It is not necessary to pass in options that will match the default value -- however, it can be useful to do so as a form of documentation. For example, when-ever I open a serial port, I explicitly set the port speed instead of relying on the default, because doing that saves me and the readers from having to search the documentation to find out what the default is in that situation.
Also, explicitly passing an option can be useful in reducing the amount of work to switch between values. For example if I think I might be switching back and forth between PreserveVariableNames true or false, then it is easier to write in the option and whatever the current value is, and then I only have to switch between true and false in the code, instead of having to delete the option each time I happen to want the behaviour that matches the default and re-enter it each time that I want a different behaviour.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!