【问题标题】:Problems with fprintf format (Matlab)fprintf 格式的问题(Matlab)
【发布时间】:2021-03-30 03:33:40
【问题描述】:

我想更正 txt 文件中变量的格式(显示在末尾,用空格替换制表符空格),使用下一个 Matlab 代码(之前导入):

id = fopen('datoscorfecha.txt', 'w');
fprintf(id, '%5s %3s %3s %3s %4s %3s %6s\n',...
'fecha', 'dia','mes', 'ano', 'hora', 'min', 'abs370');

datos = cat(2,dia, mes, ano, hora, min1, abs370);
datos = datos';
fecha = Fecha'; % Imported as a string 

fprintf(id, '%16s %2i %2i %4i %2i %2i %8.4f\n',...
    fecha, datos);
    
fclose(id);
type datoscorfecha.txt

但我收到此错误:

使用 fprintf 时出错 无法将“字符串”值转换为 'int64'。


Fecha dia mes ano hora min abs370

03/06/2016 00:00 3 6 2016 0 0 29.356218

03/06/2016 00:05 3 6 2016 0 5 30.45703

03/06/2016 00:10 3 6 2016 0 10 27.53877

03/06/2016 00:15 3 6 2016 0 15 23.19832

03/06/2016 00:20 3 6 2016 0 20 22.333924

03/06/2016 00:25 3 6 2016 0 25 22.086426

03/06/2016 00:30 3 6 2016 0 30 20.933898

【问题讨论】:

  • 请阅读minimal reproducible example。该错误抱怨fecha 和/或datos 的内容,但我们不知道这些变量包含什么。
  • @CrisLuengo 。请阅读所有问题。
  • 我已经阅读了整个问题。我无法运行你的代码,我无法告诉你如何修复它。
  • @CrisLuengo。我明确了所有变量和分隔符,所以我不知道你需要什么。也许您需要导入(使用 Matlab 接口)然后使用代码。请检查一下。
  • datos = cat(2,dia, mes, ano, hora, min1, abs370); 行有 6 个未定义的变量。帖子底部的部分没有定义这些变量是如何定义的:它们都是字符串吗?一个是datetime 对象,另一个是双打对象吗?有一些整数吗?这些是数值数组还是元胞数组?请编写为这些变量分配一些值的代码,以便我们重现您的问题。

标签: matlab import printing format fopen


【解决方案1】:

也许这样的事情可以让你用制表符替换空格。在这里,我使用textscan() 函数读取文本文件并分隔列。我还将每个值/术语解析为字符串。通过使用writematrix() 函数,我可以将数据写入一个新的文本文件,但将Delimeter 设置为tab

Text.txt(输入)

Fecha dia mes ano hora min abs370
03/06/2016 00:00 3 6 2016 0 0 29.356218
03/06/2016 00:05 3 6 2016 0 5 30.45703
03/06/2016 00:10 3 6 2016 0 10 27.53877
03/06/2016 00:15 3 6 2016 0 15 23.19832
03/06/2016 00:20 3 6 2016 0 20 22.333924
03/06/2016 00:25 3 6 2016 0 25 22.086426
03/06/2016 00:30 3 6 2016 0 30 20.933898

datoscorfecha.txt(输出)

Fecha   dia mes ano hora    min abs370  
03/06/2016  00:00   3   6   2016    0   0   29.3562
03/06/2016  00:05   3   6   2016    0   5   30.4570
03/06/2016  00:10   3   6   2016    0   10  27.5388
03/06/2016  00:15   3   6   2016    0   15  23.1983
03/06/2016  00:20   3   6   2016    0   20  22.3339
03/06/2016  00:25   3   6   2016    0   25  22.0864
03/06/2016  00:30   3   6   2016    0   30  20.9339

完整脚本:

File_ID = fopen("Text.txt");

Data = textscan(File_ID, '%s %s %s %s %s %s %s %s', 'Delimiter',' ');
fclose(File_ID);

% Data = readtable("Text.txt");
Column_1 = string(Data{:,1});
Column_2 = string(Data{:,2});
Column_3 = string(Data{:,3});
Column_4 = string(Data{:,4});
Column_5 = string(Data{:,5});
Column_6 = string(Data{:,6});
Column_7 = string(Data{:,7});
Column_8 = string(Data{:,8});

for Index = 2: length(Column_8)

Number = str2double(char(Column_8(Index,1)));
Number = num2str(Number);
Decimal_String = split(Number,".");
Decimal_String = Decimal_String{2};

if length(Decimal_String) ~= 4
Number = string(Number) + "0";
end

Column_8(Index,1) = Number;
    
end

Table = [Column_1 Column_2 Column_3 Column_4 Column_5 Column_6 Column_7 Column_8];
writematrix(Table,"datoscorfecha.txt",'Delimiter','tab');

type datoscorfecha.txt
    

使用 MATLAB R2019b 运行

【讨论】:

  • 谢谢,因为我不知道这种导入导出数据的方式,但是这个答案与我的问题无关。我想更正变量的格式,特别是最后一列,浮点数在行之间发生变化。
  • 您希望最后一列格式化为什么类型?
  • 您只需要 4 位小数?
  • 是的,我这样做了,因为我正在使用 Matlab 来纠正之前在 Fortran 中导入的 txt 数据。我需要一个常规输出的结构。
  • 我将代码更新为仅包含 4 个小数点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多