本文介绍了Matlab fopen,是否可以使用数字文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

ptol = [2, 4, 8, ...];

a = ptol(1)

fid = fopen( a,'r');

我需要打开一个文件,该文件由 ptol 调用的号码确定,即,如果 ptol(1) = 2,则 fopen 应该打开文件2.

I need to open a file determined by which number is called from ptol, i.e. if ptol(1) = 2, then fopen should open file 2.

当前,我收到错误文件名无效".我该如何解决?

Currently I get the error "invalid filename". How do I fix this?

下面的代码是我需要用来将要加载到矩阵中的文件中的数据加载"到

The following code is what I need to use to "load" the data in the files I'm struggling to open in to a matrix.

fileName = strcat(num2str(a),'.ext');
file = fopen(fileName,'r');

count = 1;

lines2skip = 4;

mat = zeros(29,872);

while ~feof(file)
    if count <= lines2skip
        count = count+1;
        [~] = fgets(file); % throw away unwanted line
        continue;
    else
        line = strtrim(fgets(file));
        mat = [mat ;cell2mat(textscan(line, '%f')).'];
        count = count +1;
    end
end

我猜

推荐答案

a 是一个数字.

因此,您需要指定与文件名相对应的字符串.该文件有扩展名吗? num2str strcat 应该具有魔力.

Thus, you need to specify a string which corresponds to the file name.Does the file have any extension? num2str and strcat should do the magic.

代码:

fileName = strcat(num2str(a),'.ext');
fid = fopen(fileName,'r');

请注意,必须将 .ext 扩展名替换为 actual 扩展名.如果您使用的是 .txt 文件,请替换为 .txt .

Notice that .ext has to be replace with the actual extension. If you are using .txt files, then replace with .txt.

还要检查文件的位置(您需要指定确切的路径).

Also, check for the position of the file (you need to specify the exact path).

这篇关于Matlab fopen,是否可以使用数字文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:42
查看更多