Matlab,避免空行

时间:2014-05-04 19:05:17

标签: matlab

我有一个 正在加载文本文件的function [Q,A] = load_test(filename)。我希望函数跳过空行,但我不知道该怎么做。

我尝试过使用

~isempty(x), ~ischar(x)

但我一直收到错误消息。到目前为止我的代码是:

fid = fopen(filename);
data = textscan(fid, '%s','delimiter','\n');
fclose(fid);

Q = cellfun(@(x) x(1:end-2), data{1}, 'uni',0);
A = cellfun(@(x) x(end) == 'T' || x(end) == 'F' && ~isempty(x),data{1});

我需要做什么?

1 个答案:

答案 0 :(得分:1)

<强>代码

%%// Your code
fid = fopen(filename);
data = textscan(fid, '%s','delimiter','\n')
fclose(fid);

%%// Additional code
%%// 1. Remove empty lines
c1 = ~cellfun(@isempty,data{:})
t1 = data{:,:}(c1,:)

%%// 2. Select only the lines that have F or T as end characters
lastInLine = regexp(t1,'.$','match','lineanchors') %%// Get the end characters

%%// Get a binary array of rows that have F or T at the end
c2 = strcmp(vertcat(lastInLine{:}),'F') | strcmp(vertcat(lastInLine{:}),'T')

%%// Finally select those rows/lines
data = {t1(c2,:)}

请注意,我不确定您是否还需要QA