Octave:使用变量filename以迭代方式从目录中读取多个文件 [英] Octave: read multiple files from a directory in an iterative way using variable filename

查看:502
本文介绍了Octave:使用变量filename以迭代方式从目录中读取多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录包含2550个文本文件。我想将这些文件作为单元数组file_contents的单独数组元素读取。一次读取一个文件很容易,即1.txt到file_contents {1},依此类推。我想创建一个循环,将所有这些文件读入file_contents单元格数组的相应元素。



我尝试过:



我试过的代码:



 file_contents = {}; 

for i = 1:2551
file_contents {i} = readFile('%d.txt',i);
结束





运行上面的代码会给我一个错误:



错误:fscanf:无效的流号= -1 





这是我正在使用的readFile函数:



 function file_contents = readFile(filename)

fid = fopen(filename);
如果fid
file_contents = fscanf(fid,'%c',inf);
fclose(fid);
else
file_contents ='';
fprintf('无法打开%s \ n',文件名);
结束

结束

解决方案

 file_contents {i} = readFile(' %d.txt',i); 



您将两个参数传递给 readFile 函数,但它只接受第一个参数。因此,当您尝试调用 fopen('%d.txt'); 时,没有此类文件,该函数返回-1。然后调用 fscanf(-1,...),因为没有这样的打开文件流而失败。



您需要更改 readFile 函数以接受这两个参数,并将它们组合以形成有效的文件名。您还需要检查从返回的值fopen 不等于-1,表示错误。


您正在调用函数

 readFile('%d.txt',i)

带有2个参数,该函数是

函数file_contents = readFile(filename)

。注意有什么不对吗?

+++++ {round 2] +++++

引用:
是!我传递2个参数,而函数只接受1.

有关我如何解决它的任何建议吗?



你在开玩笑吗?这是你的代码,你的功能。首先,问问自己,这两个参数的用途是什么,如果它们确实需要函数来完成这项工作,那么再向readfile()函数添加一个参数,否则,从调用函数中去掉多余的参数。这是非常基本的。

从某个地方获取代码并希望它开箱即用可以让你无处可去。正确的方法是阅读相关文档并正确执行:

1. 功能基础 - MATLAB& Simulink [ ^ ]

2. 从文本文件中读取数据 - MATLAB fscanf [ ^ ]

2 。循环控制语句 - MATLAB& Simulink [ ^ ]


您收到错误,因为 fopen(filename); 失败并且因为'%而失败d.txt'不会神奇地转换为文件名。


My directory contains 2550 text files. I want to read these files as separate array elements of the cell array file_contents. Reading one file at a time is easy i.e. 1.txt into file_contents{1} and so on. I want to create a loop that will read all these files into respective element of the file_contents cell array.

What I have tried:

The code I tried:

file_contents = {};

for i = 1:2551
    file_contents{i} = readFile('%d.txt', i);
end



Running the above code gives me the error:

error: fscanf: invalid stream number = -1



Here is the readFile function I am using:

function file_contents = readFile(filename)

fid = fopen(filename);
if fid
    file_contents = fscanf(fid, '%c', inf);
    fclose(fid);
else
    file_contents = '';
    fprintf('Unable to open %s\n', filename);
end

end

解决方案

file_contents{i} = readFile('%d.txt', i);


You are passing two parameters to the readFile function, but it only accepts the first one. So when you try to call fopen('%d.txt'); there is no such file, and the function returns -1. You then call fscanf(-1, ...) which fails as there is no such open file stream.

You need to change your readFile function to accept both parameters, and combine them to form a valid file name. You also need to check the value returned from fopen is not equal to -1, which indicates an error.


You are calling a function

readFile('%d.txt', i)

with 2 arguments, and that function is

function file_contents = readFile(filename)

.Notice any thing amiss?
+++++{round 2]+++++

Quote:

Yes! I am passing 2 arguments while the function only accepts 1.
Any suggestions as to how I may resolve it?


Are you kidding? It is your code, your function. First, ask yourself, what purposes those 2 arguments are, if they are really needed for the function to do the job, then add one more argument to the readfile() function, otherwise, get rid of the redundant one from the calling function. This is very basic.
Getting code from somewhere and hoping it to work out-of-the-box is getting you nowhere. The correct way is to read up on the relevant documentation and do it correctly:
1. Function Basics - MATLAB & Simulink[^]
2. Read data from text file - MATLAB fscanf[^]
2. Loop Control Statements - MATLAB & Simulink[^]


You get the error because fopen(filename); fails and it fails because '%d.txt' do not magically transform to a filename.


这篇关于Octave:使用变量filename以迭代方式从目录中读取多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆