keeping the variable names same
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Walter Roberson
2022-1-25
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 个评论
Walter Roberson
2022-1-25
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!