【问题标题】:How to save data to a text file in a specific format in Matlab?如何在 Matlab 中以特定格式将数据保存到文本文件中?
【发布时间】:2012-12-12 20:43:48
【问题描述】:

我是 Matlab 的初学者,我需要帮助以这种格式输出我的 X 和 Y 坐标:

X-142232Y77639
X-148878Y63979
X-154215Y49757
X-158196Y35097

这是一列XY 字符串。 现在我有了这个:

fileID = fopen('matrix.txt','a+' );
formatSpec = 'x'%6.4f\n';
fprintf(fileID,'%6.4f\n');
fprintf(fileID,'%6.4f\n',x, y);
fclose(fileID);

在formatSpec我不知道怎么加Y? 谢谢!

【问题讨论】:

    标签: file matlab text format


    【解决方案1】:

    将其他标记(在您的情况下为%6.4f)添加到您的格式字符串:

    formatSpec = 'X%6.4fY%6.4f\n';
    

    然后在调用fprintf时使用该格式字符串

     fprintf(fileID, formatSpec, x, y);
    

    (Documentation here.)

    【讨论】:

    • 太棒了。如果它是您正在寻找的并且是完整的,请投票并接受答案。
    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多