在MATLAB中跳过阅读字符串 [英] Skip reading strings in MATLAB

查看:518
本文介绍了在MATLAB中跳过阅读字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用 xlsread 在MATLAB中有简单的命令可以防止程序在读取字符时崩溃吗? >读取一个(20 400)矩阵数据,第一行和第一列因为有标题而忽略
,所以:

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

code> data ,大小为(19 399)。

我有一个问题,有些单元格缺少数据, 'missing',在一些数据集上我有头文件重新出现在中间。



有没有一种方法可以跳过这些字符没有程序崩溃,我不得不打开文件excel和删除这些字段?



谢谢




后期更新。这里是我正在使用的代码:

$ p $ [a,patha] = uigetfile({'*。csv'},'Select文件','c:\Data\2010');
file1 = [patha a];

%#得到一个文件ID
fid = fopen(file1,'rt');
newf = textscan(fid,['%s'repmat('%f',1,27)],'HeaderLines',1,'Delimiter',',');
fclose(fid);

%//创建第一列的日期数据
time = datenum(newf {1});

%//找出每行的分钟数
timeDiff = round(diff(datenum(time)*(24 * 60)));

%//其余数据
newf = cell2mat(newf(2:28));

我得到的错误是:

  ???错误使用==> cat 
CAT参数维度不一致。

错误==> cell2mat at 81
m {n} = cat(2,c {n,:});

错误==> testprogram at 31
pwr = cell2mat(newf(2:28));

这是由于我选择的文件中的字符。它会消失,当我手动删除它们

解决方案

如果字符串不符合期望,文本扫描失败。空白条目导致链接时出现问题 - 您的数组将有不平衡的列数。

  textscan('bla,5.4,缺少3,3,3,4','%s%f%f%f%f%f','Delimiter',',')

ans =

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



<但是,您可以使用'TreatAsEmpty',将'missing'视为空(即它们被NaNs取代)。

pre $ 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]

这可以让你运行cell2mat而不会出现问题。


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.

这篇关于在MATLAB中跳过阅读字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆