I want to add a column of numbers to a text file which already exists, how?

1 回表示 (過去 30 日間)
Nesha Wright
Nesha Wright 2018 年 7 月 11 日
コメント済み: Adam Danz 2018 年 7 月 11 日
I have a file with columns headers years, emissions and I want to add 'land'. The years run from 1750-2014. For the years 1959-2014 I want to add to this 'land column' a set of values. How do I do this without ruining or having to recreate the rest of the file?
  3 件のコメント
Nesha Wright
Nesha Wright 2018 年 7 月 11 日
The text file is .txt
Adam Danz
Adam Danz 2018 年 7 月 11 日
Here are responses to the same question. If there are any follow up questions I'd be glad to chip in.

サインインしてコメントする。

採用された回答

dpb
dpb 2018 年 7 月 11 日
Short answer is to just create the file anew...
t=readtable('yourfile'); % or whatever way to read; readtable is pretty flexible
land=nan(height(t,1)); % allocate for the new data column
ix=(t.years==1750); % find where to put data in array
land(ix:end)=NEWLANDDATA; % store the new values from wherever they come
t=[t land]; % augment the table
Save the file in whatever form you wish.
While it seems superficially to be better to only write new data, the effort to do so with sequential files is much more than simply reading the existing file, doing whatever machinations needed to the data in memory and then rewriting the file. While the other is possible for everything except truly humongous files it's not worth the effort.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by