且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用sscanf或fscanf从文件中读取字符串?

更新时间:2023-11-13 22:14:46

您正在寻找的功能应该是

The function you are looking for would rather be textscan

它将允许您一次读取整个文件,并指定要读取的每一列的格式.

It will allow you to read the whole file at once and to specify the format of each column you are reading.

您的情况应该是这样的:

In your case, it should be something like :

fileID = fopen('myfile.txt');
C = textscan(fileID, '%f %c %s');
fclose(fileID);
celldisp(C)

由于我们不确定您的文本文件的格式,因此您可能需要调整Format字符串.

As we don't know exactly the format of your text file, you'll probably have to tweak the Format string.

您将获得一个单元格数组,其中包含根据格式规范分析的文件内容.

You will get a cell array with the content of the file parsed according to the format specification.

Matlab的文档详细说明了可能性,格式规范,并提供了很好的示例.

Matlab's documentation explain in details the possibilities, the format specification, and comes with good examples.