matlab如何从excel表格中读取数据?

如题所述

第一种方法,你可以使用xlsread函数来读取excel中的数据

第二种方法,就是把字符转化为数字,使用函数str2num

xlsread的使用
EXAMPLES:
1. Default operation:
NUMERIC = xlsread(FILE);
[NUMERIC,TXT]=xlsread(FILE);
[NUMERIC,TXT,RAW]=xlsread(FILE);

2. Get data from the default region:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet')

3. Get data from the used area in a sheet other than the first sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2')

4. Get data from a named sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData')

5. Get data from a specified region in a sheet other than the first
sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2','a2:j5')

6. Get data from a specified region in a named sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData','a2:j5')

7. Get data from a region in a sheet specified by index:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet',2,'a2:j5')

8. Interactive region selection:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet',-1);
You have to select the active region and the active sheet in the
EXCEL window that will come into focus. Click OK in the Data
Selection Dialog when you have finished selecting the active region.

祝你学习愉快!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-29

第一种方法,使用xlsread函数来读取excel中的数据

第二种方法,就是把字符转化为数字,使用函数str2num


MATLAB读取Excel表格数据和处理数据

    分步阅读

    本文以MATLAB读取某考试成绩的Excel表格数据为例,然后计算出各科的平均成绩、最高分、最低分和各位同学的总成绩。

    工具/原料

    MATLAB

    Excel

    xlsread

    方法/步骤

    第一,准备数据。下图是Amy,John,Julia,Kite四位同学的数学(Mathematics)和英语(English)考试成绩,保存在名为results.xlsx的Excel表格中,作为本次MATLAB读取的数据。

    第二,启动MATALB,新建脚本(Ctrl+N),输入如下代码:

    close all; clear all; clc

    A=xlsread('results.xlsx')

    其中,由于results.xlsx数据存放位置与MATLAB设置的路径一致,所以本文直接就写成xlsread('results.xlsx')。如果路径不一致,应该写成xlsread('路径\results.xlsx')的形式,例如xlsread('D:\Matlab\MATLAB2017\results.xlsx')的形式。

    第三,保存和运行上述脚本,Excel表格中的数据就被读入A中,在工作区可以看到A的值(双击可以打开),在命令行窗口也可以看到如下结果:

    A =

    99    73

    85    95

    62    86

    55    45

    第四,下面计算四位同学数学(Mathematics)和英语(English)的平均成绩、最高分、最低分和各位同学的总成绩。接着输入如下代码:

    format compact

    Maths_mean=mean(A(:,1))

    Maths_max=max(A(:,1))

    Maths_min=min(A(:,1))

    English_mean=mean(A(:,2))

    English_max=max(A(:,2))

    English_min=min(A(:,2))

    Amy_sum=sum(A(1,:)),John_sum=sum(A(2,:))

    Julia_sum=sum(A(3,:)),Kite_sum=sum(A(4,:))

    其中,format compact表示行间距紧凑(一会儿在命令行窗口中会看到)。A(:,1)表示第一列,也就是四位同学的数学(Mathematics)成绩,A(1,:)表示第一行,也就是Amy的数学(Mathematics)和英语(English)成绩。其他依次类推。

    第五,保存和运行上述脚本,得到如下结果:

    Maths_mean =

    75.2500

    Maths_max =

    99

    Maths_min =

    55

    English_mean =

    74.7500

    English_max =

    95

    English_min =

    45

    Amy_sum =

    172

    John_sum =

    180

    Julia_sum =

    148

    Kite_sum =

    100

    可以看出,四位同学的数学(Mathematics)平均分为75.25,最高分99,最低分55,英语(English)平均分为74.75,最高分95,最低分45,还以看到Amy总分172分,John总分180分,Julia总分148分,Kite总分100分。

    END

    注意事项

    format compact起到使行间距紧凑的作用。

    mean()求平均值,max()求最大值,min()求最小值,sum()求和。

本回答被网友采纳
第2个回答  2018-03-11

第一种方法,你可以使用xlsread函数来读取excel中的数据

第二种方法,就是把字符转化为数字,使用函数str2num

xlsread的使用

EXAMPLES:

1. Default operation: 

NUMERIC = xlsread(FILE);

[NUMERIC,TXT]=xlsread(FILE);

[NUMERIC,TXT,RAW]=xlsread(FILE);

2. Get data from the default region:

NUMERIC = xlsread('c:\matlab\work\myspreadsheet')

3. Get data from the used area in a sheet other than the first sheet:

NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2')

4. Get data from a named sheet:

NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData')

5. Get data from a specified region in a sheet other than the first

sheet:

NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2','a2:j5')

6. Get data from a specified region in a named sheet:

NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData','a2:j5')

7. Get data from a region in a sheet specified by index:

NUMERIC = xlsread('c:\matlab\work\myspreadsheet',2,'a2:j5')

8. Interactive region selection:

NUMERIC = xlsread('c:\matlab\work\myspreadsheet',-1);

You have to select the active region and the active sheet in the

EXCEL window that will come into focus. Click OK in the Data 

Selection Dialog when you have finished selecting the active region.

第3个回答  2018-02-02
第一种方法,你可以使用xlsread函数来读取excel中的数据

第二种方法,就是把字符转化为数字,使用函数str2num

xlsread的使用
EXAMPLES:
1. Default operation:
NUMERIC = xlsread(FILE);
[NUMERIC,TXT]=xlsread(FILE);
[NUMERIC,TXT,RAW]=xlsread(FILE);

2. Get data from the default region:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet')

3. Get data from the used area in a sheet other than the first sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2')

4. Get data from a named sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData')

5. Get data from a specified region in a sheet other than the first
sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2','a2:j5')

6. Get data from a specified region in a named sheet:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData','a2:j5')

7. Get data from a region in a sheet specified by index:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet',2,'a2:j5')

8. Interactive region selection:
NUMERIC = xlsread('c:\matlab\work\myspreadsheet',-1);
You have to select the active region and the active sheet in the
EXCEL window that will come into focus. Click OK in the Data
Selection Dialog when you have finished selecting the active region.

祝你学习愉快!
相似回答