2017-03-21 870 views
0

我试图读取文件的顺序与textscan有效的文件标识符,但我不断收到此错误:无效的文件标识符。使用的fopen产生

Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.

我的代码是:

fd = fopen(['D:\Thesis\Data\vedai_co_75_25\train\labels\' files(id).name],'rt'); 
line = textscan(fd, '%s %f %d %f %f %f %f %f %f %f %f %f %f %f %f'); 

当我尝试看看的['D:\Thesis\Data\vedai_co_75_25\train\labels\' files(id).name]输出返回正确路径:

D:\Thesis\Data\vedai_co_75_25\train\labels\00000000.png

还命令fopen(D:\Thesis\Data\vedai_co_75_25\train\labels\00000000.png)返回或或等

但是命令fopen(['D:\Thesis\Data\vedai_co_75_25\train\labels\' files(id).name])返回-1

我该如何做这项工作?

+1

仅供参考,我的系统上这两个命令工作,不返回** - ** 1(有或无字符串连接)。我在Linux上,似乎你在Windows上。 – UJIN

+0

@UJIN是的,我正在使用Windows。第二个命令在** for循环**中,所以我不能像第一个那样使用它。 –

+0

是的,我知道你需要使用第二个版本。不幸的是,我无法重现您所遇到的行为,两者对我而言都很好。您将不得不等待可以使用Windows的人员,也可能使用Windows。 – UJIN

回答

2

问题可能是您试图在文本模式下打开.png文件。
尝试fd = fopen(file_name, 'r'),或在你的榜样:

fd = fopen(['D:\Thesis\Data\vedai_co_75_25\train\labels\' files(id).name],'r'); 

在Windows上,在文本模式下打开了错误的文件可能会导致问题。的fopen各国帮助:

(On Unix, text and binary mode are the same, so this has no effect. On PC systems this is critical.)

+0

是的,一定是这个。当我在fopen中看到一个图像中的'rt'时,我感到有些困惑,但后来我在Ubuntu上进行了测试,结果没有返回任何错误。 – UJIN

+0

是的,谢谢你在Linux上进行测试。这导致了我的假设。 – Richard

+0

我对这个错误感到非常抱歉。我应该使用'txtfiles'而不是'files'。问题解决了。非常感谢 –