如何读取Excel文件?

46次观看(最近30天)
Sarlota Duskova
Sarlota Duskova 2020年5月10日
Edited: dpb 2020年5月14日
Hello,
I am using Matlab R2019a and I have this weird excel file. I want to skip first six header lines, then in first column I have date but is only in first row, then starts second day and again the date is only in one row and I want to have all rows contains that date till it starts second day and all rows will contains that day, second column is time but again first row contains date and time but others rows have only time. Then I have few columns with data but some columns are empty. How to create table with date and time and columns with data without empty columns and without the first six header lines? Is it possible? My code for example what I have, but it changed data. I want to compare this Excel with another data which I already processed and then I want to plot date and time and one column with data from this Excel file, so that is the reason why I need create new table with usable data. Thank you for your reply.
[filename,path] = uigetfile(...
{'*.xls; *.xlsx',,,,'Microsoft Excel file (*.xls, *.xlsx)'},“加载文件”,,,,'MultiSelect',,,,'上');
[num,txt,raw] = xlsread(fullfile(path,filename));
data = cellfun(@mean,raw);
a = data(:,〜所有(isnan(data)));

Accepted Answer

dpb
dpb 2020年5月10日
Edited:dpb 2020年5月14日
阅读并不难;解释您缺乏一些信息...
First, use the 检测iMportoptions function to set up a base import object and then pick only the columns with variable names to actually read in --
opt = dentectimportoptions('test_file.xls',,,,“ numheaderlines”,4);%实际上只有5个,没有六个标题线
isgoodcol = find(cellfun(@isempty,regexp(opt.SelectedVariablenames,'Var*'));%查找定义的变量名称
opt.SelectedVariableNames=opt.SelectedVariableNames(isGoodCol);%仅导入这些列
t =可读取('test_file.xls',,,,opt);%阅读桌子
[h,m,s]=hms(datetime(T.TIME_,“转换”,,,,'excel'));%从Excel转换时间
t.date_ = dateshift(fillmissing(t.date_,,'以前的'),'开始',,,,'天');%开始一天
T.DATE_=T.DATE_+duration(h,m,s);% and add time
T.Time _ = [];% now superfluous
Returns
T =
43×9 table
DATE_m00__cM01_CM02__CM03__CM04__CM10__HM20__CM30_Gk
____________________________________________________________________
10-MAR-2020 14:05:36 24.59 23.50 23.43 23.58 -1.09 28.10 4.90 5.40
10-Mar-2020 14:10:36 24.51 23.48 23.44 23.61 -1.03 28.00 4.80 5.30
10-Mar-2020 14:15:36 24.43 23.42 23.43 23.60 -1.01 28.50 5.00 5.40
...
10-MAR-2020 16:10:36 24.29 23.27 23.38 23.57 -1.02 28.20 4.70 5.30
10-Mar-2020 16:15:36 24.29 23.27 23.38 23.56 -1.02 28.20 4.70 5.30
10-Mar-2020 16:20:36 24.29 23.27 23.38 23.56 -1.02 28.30 4.80 5.30
10-MAR-2020 16:25:36 24.29 23.27 23.38 23.56 -1.02 28.30 4.80 5.30
11月11日2020 00:00:36 24.28 23.24 23.34 23.53 -1.04 29.00 5.10 5.10 5.40
11-Mar-2020 00:05:36 24.29 23.24 23.34 23.53 -1.05 29.00 5.10 5.40
11-Mar-2020 00:10:36 24.28 23.24 23.33 23.53 -1.04 29.10 5.20 5.40
11-Mar-2020 00:15:36 24.26 23.24 23.34 23.53 -1.02 29.10 5.10 5.40
11-Mar-2020 00:50:36 24.26 23.24 23.34 23.53 -1.02 29.10 5.10 5.40
11-Mar-2020 00:55:36 24.28 23.24 23.34 23.53 -1.04 29.10 5.20 5.40
11月11日2020 01:00:36 24.26 23.24 23.34 23.53 -1.02 29.10 5.20 5.20 5.40
11月11日2020 01:05:36 24.28 23.24 23.33 23.53 -1.04 29.20 5.20 5.20 5.40
...
>>
where I elided data rows for brevity.
3条评论
dpb
dpb 2020年5月14日
No problem...easy to get lost in the weeds w/ newer features -- there are so many functions that until have seen one or more it's hard to know what tools are around...the dateshift 是这里的关键;您的拾起并使用 不见了 是/是很好的开始。
我还没有回去看一下Excel中的实际文件,因此在第一个回合的表单上whif着excel的时间...甚至没有想到它,因为有一个实际的日历日期/时间。认为这是其他一些相对柜台...
The key thing to remember w/ datetime 对尊贵 datenum datetime 与日期组件相比,与日期组件不同,这一天是整数零件和一天的时间分数。因此,您必须使用 期间 在一天中的时间,无论是否有一天的参考 - 有时您可以通过视觉格式掩盖输出格式,但在内部,它将始终具有真实日期或当前日期的参考日期。
Hence the shift back to midnight/beginning of the day for the dates and then throwing away the date part of the time to be able to combine the pieces into a real date.

登录发表评论。

更多答案(0)

下载188bet金宝搏


Release

R2019a

社区Treasure Hunt

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

Start Hunting!