【问题标题】:Save current state of .m file, write to file later (Matlab)保存 .m 文件的当前状态,稍后写入文件(Matlab)
【发布时间】:2016-06-16 01:44:32
【问题描述】:

我的问题:我可以将 .m 文件的状态保存为变量,以便稍后在代码中写入文件吗?

目前,我有以下几行:

source_file     = mfilename('fullpath');
write_path      = '~/data';

(code that takes many minutes to execute)

copyfile([source_file,'.m'],[write_path,'/source_file.m']);

问题在于,在代码执行的几分钟或几小时内,我将对原始 .m 代码进行许多编辑。在文件末尾调用copyfile 时,它会保存修改后的代码,而不是已执行的代码。我当然意识到我可以在大部分代码之前调用copyfile,但我不想这样做。

【问题讨论】:

    标签: matlab io save file-copying


    【解决方案1】:

    内存中加载的文件仍然是原始文件,但磁盘上的文件现在是修改后的文件。您最好的选择是在代码开头调用copyfile(不知道为什么不能这样做)。如果由于某种原因你真的不能这样做,你可以使用 fread 读取源代码,然后在脚本完成后将相同的字符串 out 写入另一个文件。

    fid = fopen(source_file, 'r');
    source_code = fread(fid);
    fclose(fid);
    
    % Do stuff
    
    fout = fopen(fullfile(write_path, 'source_file.m'), 'w');
    fwrite(fout, source_code);
    fclose(fout)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2014-07-25
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多