matlab迭代文件名保存

matlab迭代文件名保存

本文介绍了matlab迭代文件名保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于matlab的这个问题:
我正在运行一个循环,每次迭代产生一组新的数据,并且我希望每次都将它保存在一个新文件中。我也通过更改名称来覆盖旧文件。看起来像这样:

$ $ p $ name $ c $ name name_each_iter = strrep(some_source,'。string.mat','string_new。(j).mat' )

以及我在这里挣扎的是迭代,以便获得文件:
... string_new.1.mat
... string_new.2.mat
等。

我试着用各种组合() [] {}以及'string_new.'j'.mat'(它给出了语法错误)

如何做?

解决方案

要创建一个基于已经存在的文件的名字,你可以使用来检测'_new。(number).mat',并根据regexp的发现改变字符串:

  original_filename ='data.string.mat'; 
im = regexp(original_filename,'_ new.\\d + .mat')
if isempty(im)%original file,no _new。(j)detected
newname = [original_filename(1 :end-4)'_new.1.mat'];
else
num = str2double(original_filename(im(end)+5:end-4));
newname = sprintf('%s_new。%d.mat',original_filename(1:im(end)-1),num + 1);
end

这个确实如此,并且产生了:

  data.string_new.1.mat 
data.string_new.2.mat
data.string_new.3.mat
。 ..

data.string_new.9.mat
data.string_new.10.mat
data.string_new.11.mat

当迭代上面的函数时,以'data.string.mat'开始


this question about matlab:i'm running a loop and each iteration a new set of data is produced, and I want it to be saved in a new file each time. I also overwrite old files by changing the name. Looks like this:

name_each_iter = strrep(some_source,'.string.mat','string_new.(j).mat')

and what I#m struggling here is the iteration so that I obtain files:...string_new.1.mat...string_new.2.matetc.

I was trying with various combination of () [] {} as well as 'string_new.'j'.mat' (which gave syntax error)

How can it be done?

解决方案

For creating a name based of an already existing file, you can use regexp to detect the '_new.(number).mat' and change the string depending on what regexp finds:

original_filename = 'data.string.mat';
im = regexp(original_filename,'_new.\d+.mat')
if isempty(im) % original file, no _new.(j) detected
    newname = [original_filename(1:end-4) '_new.1.mat'];
else
    num = str2double(original_filename(im(end)+5:end-4));
    newname = sprintf('%s_new.%d.mat',original_filename(1:im(end)-1),num+1);
end

This does exactly that, and produces:

    data.string_new.1.mat
    data.string_new.2.mat
    data.string_new.3.mat
    ...

    data.string_new.9.mat
    data.string_new.10.mat
    data.string_new.11.mat

when iterating the above function, starting with 'data.string.mat'

这篇关于matlab迭代文件名保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:38