Matlab 有没有办法识别空行?

发布于 2024-09-06 06:41:29 字数 54 浏览 3 评论 0原文

在Matlab中扫描文本文件时有没有办法识别空行?我想根据文本之间的空行来解析文件。这可能吗?

Is there a way to recognize blank lines when you are scanning a text file in Matlab? I want to parse the files based on the blank lines in between the text. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-09-13 06:41:29

是的,这是可能的。 MATLAB 代码片段如下所示:

fid = fopen('reader.m');

newline = sprintf('\r\n');
line = fgets(fid);
while ischar(line)
    if strcmp(newline, line)
        disp('Empty line');
    else
        disp('Non-empty line');
    end
    line = fgets(fid);
end

Yes, it's possible. A MATLAB snippet would look something like:

fid = fopen('reader.m');

newline = sprintf('\r\n');
line = fgets(fid);
while ischar(line)
    if strcmp(newline, line)
        disp('Empty line');
    else
        disp('Non-empty line');
    end
    line = fgets(fid);
end
隱形的亼 2024-09-13 06:41:29

这是一种可能性:

fid = fopen('myfile.txt');
lines = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
lines = lines{1};
% lines now contains a cell array of strings,
% one per line in the file.

% Find all the blank lines using cellfun:
blank_lines = find(cellfun('isempty', lines));

Here's one possibility:

fid = fopen('myfile.txt');
lines = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
lines = lines{1};
% lines now contains a cell array of strings,
% one per line in the file.

% Find all the blank lines using cellfun:
blank_lines = find(cellfun('isempty', lines));
栀梦 2024-09-13 06:41:29

没有 \r...现在工作正常

fid = fopen('reader.m');

newline = sprintf('\n');
line = fgets(fid);
while ischar(line)
    if strcmp(newline, line)
        disp('Empty line');
    else
        disp('Non-empty line');
    end
    line = fgets(fid);
end

without \r...now works fine

fid = fopen('reader.m');

newline = sprintf('\n');
line = fgets(fid);
while ischar(line)
    if strcmp(newline, line)
        disp('Empty line');
    else
        disp('Non-empty line');
    end
    line = fgets(fid);
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文