Make changes to a text file with numbers and text

4 ビュー (過去 30 日間)
Zoe Zimmerman
Zoe Zimmerman 2023 年 4 月 27 日
コメント済み: Zoe Zimmerman 2023 年 4 月 27 日
Hi,
I would like to:
  • import the attached text file into matlab
  • multiply all numeric values of the attached text file by 0.89
Unfortunately, I am having trouble with this as the headings are different for each section of number values.

回答 (1 件)

chicken vector
chicken vector 2023 年 4 月 27 日
編集済み: chicken vector 2023 年 4 月 27 日
str = fileread('hector_400km.txt');
lines = regexp(str, '\r\n|\r|\n', 'split');
for line = 20 : length(lines)
modifiedData = str2num(lines{line})*0.89;
% This check is done because I noticed some lines (e.g. 620) are not data to be
% multplied:
if ~isempty(modifiedData)
nSpaces = 6 - numel(num2str(floor(modifiedData(1))));
lines{line} = [repmat(' ',1,nSpaces) num2str(modifiedData,'%9.2f')];
end
end
fid = fopen('hector_400km_new.txt','w');
fprintf(fid,'%s\n',lines{:});
fclose(fid);
This should preserve the spaces and the formatting, otherwise, if you don't care about that the code can be simplified and you can write a .csv with writecell.
  1 件のコメント
Zoe Zimmerman
Zoe Zimmerman 2023 年 4 月 27 日
wow!! thank you so much this is amazing :)

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by