如何使用 fscanf 在 Matlab 中读取 .xls 文件

How to read a .xls file in Matlab using fscanf

我正在尝试将 .xls 文件读入 Matlab。我被禁止使用 built-in 函数,例如 xlsread()、textread() 和 readable()。如果更容易,我可以将 .xls 文件转换为其他格式,我已经使用 data.txt 尝试了下面的代码,但无济于事。

我可以使用基本的 'fopen'、'fscanf'、'fread' 功能。但是,我似乎无法弄清楚如何正确实施它们。

我的数据的模拟版本如下所示....

Col1  Col2  Col3   Col4  Col5  Col6  Col7
  1     2     3      4     5.5   6    Fox
  2     3     4      5     6.7   8    Cat
  18    12    56     3     20.2  9    Dog

等等大约 400 行

目前,我的代码如下所示

fileID = fopen('data.xls', 'r');
fgetl(fileID);
data = fscanf(fileID, '%d %d %d %d %f %d %*s');
fclose(fileID);

我正在尝试删除列 header 行并仅检索前 6 列。我想忽略最后的文本栏。

这段代码returns一个空矩阵'data[]'

我做错了什么?

更新代码和数据集

我已经在整个数据集上实现了 MichaelTr7 的答案,但它仍然只复制文档的第一行?下面是对数据集的更准确描述。为了简单起见,我没有在上面包含它,但现在我相信它是需要的

Col1  Col2  Col3   Col4  Col5   Col6  Col7  Col8  Col9
  18    8    307    130   3504   12    70    1   chevrolet chevelle malibu
  2     3     4      5    6.7    8     70    1   buick skylark 320
  18    12    56     3    20.2   9     70    1   amc ambassador dpl
  NA    4    133    115   3090   40    70    2   ford torino

代码...

fileID = fopen('data.txt', 'r');
Headers = string(fgetl(fileID));
Format_Specification = '%d %d %d %d %d %f %d %d %*s';
Array_Size = [8 Inf];

Output_Array = fscanf(fileID,Format_Specification,Array_Size);
Output_Array = Output_Array.';

fclose(fileID);

Ouput_array 现在等于 [18,8,307,130,3504,12,70,1]

解释中: 此方法使用配置了特定格式的 .txt(文本文件)和 scanf()。以下和规格:

Format_Specification = '%d %d %d %d %f %d %*s';

表示为%d的是读作整数的术语。
表示为 %f 的是读作浮点数的术语。
表示为 %*s 的是忽略的字符串项。星号 * 表示要忽略的术语。

Array_Size = [6 Inf];

Array_Size 指示读入格式应如何按列读取。由于有六个项,数组的高度为 6。另一个维度是 Inf(无限)以容纳任何长度的 .txt(文本文件)。通过转置 Array_Size.' 格式可以按行显示为 expected/to 符合原始 .txt(文本文件)中的格式。

data.txt

Col1  Col2  Col3   Col4  Col5  Col6  Col7
  1     2     3      4     5.5   6    Fox
  2     3     4      5     6.7   8    Cat
  18    12    56     3     20.2  9    Dog

脚本:

clear;
clc;
fileID = fopen('data.txt', 'r');

%Grabbing the first line with headers and ignoring%
Text = string(fgetl(fileID));


Format_Specification = '%d %d %d %d %f %d %*s';
Array_Size = [6 Inf];

Output_Array = fscanf(fileID,Format_Specification,Array_Size);
Output_Array = Output_Array.';

fclose(fileID);

Output_Array

编辑:使用 fgetl()split()size()

实现

data.txt

Col1  Col2  Col3   Col4  Col5   Col6  Col7  Col8  Col9
  18    8    307    130   3504   12    70    1   chevrolet chevelle malibu
  2     3     4      5    6.7    8     70    1   buick skylark 320
  18    12    56     3    20.2   9     70    1   amc ambassador dpl
  NA    4    133    115   3090   40    70    2   ford torino

脚本:

clear;
clc;
fileID = fopen('data.txt', 'r');

Text = string(fgetl(fileID));

%Running for loop till end of file termination "-1"%
Line_Index = 1;
while(Text ~= "-1") 
    
    Text = string(fgetl(fileID));
    if (Text ~= "-1")
    Lines(Line_Index,1) = Text;
    end

    Line_Index = Line_Index + 1;
end


fclose(fileID);

Output_Array = [];
[Number_Of_Lines,~] = size(Lines);

for Row_Index = 1: Number_Of_Lines

Line = split(Lines(Row_Index,:));
Line = Line(2:9)';

Output_Array(Row_Index,:) = Line(1,:);


end