matlab 行 读取文件 跳过_在MATLAB中跳过阅读字符串

Is there is easy command in MATLAB which prevents program from crashing when it reads characters?

I use xlsread to read a (20 400) matrix data , the first row and column get disregarded

as they have headers, so that:

data = xlsread ('C:\file.xls')

results in data with a size of (19 399).

I have a problem, some cells have missing data and it's written 'missing' and on some data sets i have headers reappear in middle.

Is there a way to skip these characters without the program crashing and me having to open the file in excel and deleting those fields?

Thanks

sorry for the late update. Here is the code i am using:

[a,patha]=uigetfile({'*.csv'},'Select the file' ,'c:\Data\2010');

file1=[patha a];

%# get a file ID

fid = fopen(file1,'rt');

newf= textscan(fid, ['%s' repmat('%f',1,27)], 'HeaderLines', 1, 'Delimiter', ',');

fclose(fid) ;

%//Make time a datenum of the first column

time = datenum(newf{1} );

%//Find the difference in minutes from each row

timeDiff = round(diff(datenum(time)*(24*60)));

%//the rest of the data

newf = cell2mat(newf(2:28));

the error i get is:

??? Error using ==> cat

CAT arguments dimensions are not consistent.

Error in ==> cell2mat at 81

m{n} = cat(2,c{n,:});

Error in ==> testprogram at 31

pwr = cell2mat(newf(2:28));

it is due to the characters in my file i selected. it disappears when i manually delete them

解决方案

Textscan fails if the string doesn't match the expectation. The empty entries lead to problems when catenating - your array would have an uneven number of columns.

textscan('bla,5.4,missing,3,3,3.4','%s%f%f%f%f%f','Delimiter',',')

ans =

{1x1 cell} [5.4000] [0x1 double] [0x1 double] [0x1 double] [0x1 double]

However, you can use'TreatAsEmpty', to treat 'missing' as empty (i.e. they're replaced by NaNs)

textscan('bla,5.4,missing,3,3,3.4','%s%f%f%f%f%f','Delimiter',',','TreatAsEmpty','missing')

ans =

{1x1 cell} [5.4000] [NaN] [3] [3] [3.4000]

This allows you to run cell2mat without problems.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值