Matlab fopen, is it possible to have a numeric file name?

為{幸葍}努か 提交于 2021-02-05 09:49:07

问题


I have the following code:

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

a = ptol(1)

fid = fopen( a,'r');

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

回答1:


a is a number, I guess.

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.

The code:

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

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).



来源:https://stackoverflow.com/questions/17837378/matlab-fopen-is-it-possible-to-have-a-numeric-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!