Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用matlab搜索文本文件中的特定单词并添加字符串?_String_Matlab_Cell - Fatal编程技术网

String 使用matlab搜索文本文件中的特定单词并添加字符串?

String 使用matlab搜索文本文件中的特定单词并添加字符串?,string,matlab,cell,String,Matlab,Cell,我在任何地方都找不到如何在文本文件中搜索特定的单词,所以这里是,这就是我现在看到的,只是阅读并打印整个文本文件。 我的文本文件包含一些单词,如: K_G_M_H_NICK_MA,07500 p_SA_开关p_MPE_VL,0,1 如果字符串以“K”开头,则必须将“spec”写在行的末尾,无论后面是什么。输出必须是 K_G_M_H_NICK_MA,07500规格 如果字符串以“p”开头,则必须在行的末尾写入“test”,无论p后面是什么。输出必须是 p_SA_开关p_MPE_VL,0,1测试不幸的

我在任何地方都找不到如何在文本文件中搜索特定的单词,所以这里是,这就是我现在看到的,只是阅读并打印整个文本文件。 我的文本文件包含一些单词,如:

K_G_M_H_NICK_MA,07500

p_SA_开关p_MPE_VL,0,1

如果字符串以“K”开头,则必须将“spec”写在行的末尾,无论后面是什么。输出必须是

K_G_M_H_NICK_MA,07500规格

如果字符串以“p”开头,则必须在行的末尾写入“test”,无论p后面是什么。输出必须是


p_SA_开关p_MPE_VL,0,1测试不幸的是,MATLAB不具备此功能。你别无选择,只能阅读每一行文本,检查需要的内容,然后保存一个新文件。可以使用从文本文件中查询行。对
fgetl
的后续调用读取下一行,并记住从中读取的最后一行。您可以将其与标准的
fopen
fclose
一起使用,以打开和关闭文件。在使用
fgetl
之前,您需要使用
fopen
引用打开的文件。假设您的文件名为
text.txt
,只需读入该行,检查第一个字符是否符合您的条件,然后在该行末尾打印出您需要的内容。明智的做法是在代码前后删减任何空格,这样可能对您有用。类似的方法应该可以工作,同时假设您希望写入名为
text\u new.txt的新文件:

fid = fopen('text.txt', 'r'); % Open up the file
fidw = fopen('text_new.txt', 'w'); % Open up a new file to write

tline = fgetl(fid); % Get the first line in the text file

while ischar(tline) % As long as this line contains characters (i.e. not end of the file)
    % Trim the whitespace
    tline = strtrim(tline);
    % Check if first character is K or k, then write spec at the end of the line
    if tline(1) == 'k' || tline(1) == 'K'
        fprintf(fidw, '%s spec\n', tline);
    % Check if first character is P or p, then write test at the end of the line
    elseif tline(1) == 'p' || tline(1) == 'P'
        fprintf(fidw, '%s test\n', tline);
    % Write the line as is if no conditions match
    else
        fprintf(fidw, '%s\n', tline);
    end

    % Get the next line in the file
    tline = fgetl(fid);
end

% Close the files now
fclose(fid);
fclose(fidw);

不幸的是,MATLAB没有这种能力。你别无选择,只能阅读每一行文本,检查需要的内容,然后保存一个新文件。可以使用从文本文件中查询行。对
fgetl
的后续调用读取下一行,并记住从中读取的最后一行。您可以将其与标准的
fopen
fclose
一起使用,以打开和关闭文件。在使用
fgetl
之前,您需要使用
fopen
引用打开的文件。假设您的文件名为
text.txt
,只需读入该行,检查第一个字符是否符合您的条件,然后在该行末尾打印出您需要的内容。明智的做法是在代码前后删减任何空格,这样可能对您有用。类似的方法应该可以工作,同时假设您希望写入名为
text\u new.txt的新文件:

fid = fopen('text.txt', 'r'); % Open up the file
fidw = fopen('text_new.txt', 'w'); % Open up a new file to write

tline = fgetl(fid); % Get the first line in the text file

while ischar(tline) % As long as this line contains characters (i.e. not end of the file)
    % Trim the whitespace
    tline = strtrim(tline);
    % Check if first character is K or k, then write spec at the end of the line
    if tline(1) == 'k' || tline(1) == 'K'
        fprintf(fidw, '%s spec\n', tline);
    % Check if first character is P or p, then write test at the end of the line
    elseif tline(1) == 'p' || tline(1) == 'P'
        fprintf(fidw, '%s test\n', tline);
    % Write the line as is if no conditions match
    else
        fprintf(fidw, '%s\n', tline);
    end

    % Get the next line in the file
    tline = fgetl(fid);
end

% Close the files now
fclose(fid);
fclose(fidw);

如果您有足够的内存来加载整个文件,您可以利用一些工具,比如一次对整个文档进行操作,这可能会在比较和文件输入/输出方面节省一些时间

对于
regexprep
示例,我们可以执行以下操作:

% Load entire data file into memory (cell array)
fID =  fopen('mydata.txt', 'r');
tmpdoc = {};
ii = 1;
while ~feof(fid)
    tmpdoc{ii} = fgetl(fID);
    ii = ii + 1;
end
fclose(fID);  % Clean up

% Make the necessary replacements
% String insensitive search
tmpdoc = regexprep(tmpdoc, '(^K.*)$', '$0 spec', 'preservecase'); % Find lines (cells) that begin with K
tmpdoc = regexprep(tmpdoc, '(^P.*)$', '$0 test', 'preservecase'); % Find lines (cells) that begin with P

% Write data
fID_new = fopen('mydata_new.txt', 'w');
fprintf(fID_new, '%s\n', tmpdoc{:});
fclose(fID_new);  % Clean up

对于2000行文件的基本计时(仅重复两行,这代表了最坏的情况),这种方法大约比其他方法快3倍,但他的方法只花了大约0.25秒,因此仍然非常快。

如果您有足够的内存加载整个文件,您可以利用诸如一次操作整个文档这样的工具,在比较和文件输入/输出方面可能节省一些时间

对于
regexprep
示例,我们可以执行以下操作:

% Load entire data file into memory (cell array)
fID =  fopen('mydata.txt', 'r');
tmpdoc = {};
ii = 1;
while ~feof(fid)
    tmpdoc{ii} = fgetl(fID);
    ii = ii + 1;
end
fclose(fID);  % Clean up

% Make the necessary replacements
% String insensitive search
tmpdoc = regexprep(tmpdoc, '(^K.*)$', '$0 spec', 'preservecase'); % Find lines (cells) that begin with K
tmpdoc = regexprep(tmpdoc, '(^P.*)$', '$0 test', 'preservecase'); % Find lines (cells) that begin with P

% Write data
fID_new = fopen('mydata_new.txt', 'w');
fprintf(fID_new, '%s\n', tmpdoc{:});
fclose(fID_new);  % Clean up

对于2000行文件的基本计时(仅重复两行,这代表了最坏的情况),这种方法大约比其他方法快3倍,但他的方法只花了大约0.25秒,所以速度仍然很快。

@Chris Taylor你能看看这个问题吗?谢谢这些文件有多大?@Chris Taylor你能看看这个问题吗?谢谢这些文件有多大?由于MATLAB通常具有较高的FIO开销,内存允许它可以更快地将所有内容读入内存,并利用类似(或者可能是
字符串
)的内容一次性进行替换。@excaza True,但我对输入文件不作任何假设。它可能非常大,这就是为什么我默认为逐行。然而,你的评论是完全正确的。如果OP更新了她的问题来谈论这些有多大,我会将此作为另一种方法添加进来,或者您可以将其作为单独的答案添加进来。该文件有ca 1100行。。两种解决方案都适合我!谢谢大家@劳拉,不客气。如果您不再需要帮助,请将我们的任一答案标记为已接受,以让社区知道您不再需要帮助。祝你好运。@rayryeng:是的,我想。。我在上面问了最后一个问题:如果我想复制这一行并在下一行中添加一些文本,那么我如何使用您的代码达到这一点?第1行:K_G_M_H_NICK_MA,07500第2行:K_G_M_H_NICK_MA,07500 spec?由于MATLAB通常具有较高的FIO开销,内存允许将所有内容读取到内存中,并利用类似于(或者可能是
字符串
)的内容一次性进行替换。@excaza True,但我对输入文件不作任何假设。它可能非常大,这就是为什么我默认为逐行。然而,你的评论是完全正确的。如果OP更新了她的问题来谈论这些有多大,我会将此作为另一种方法添加进来,或者您可以将其作为单独的答案添加进来。该文件有ca 1100行。。两种解决方案都适合我!谢谢大家@劳拉,不客气。如果您不再需要帮助,请将我们的任一答案标记为已接受,以让社区知道您不再需要帮助。祝你好运。@rayryeng:是的,我想。。我在上面问了最后一个问题:如果我想复制这一行并在下一行中添加一些文本,那么我如何使用您的代码达到这一点?第1行:K_G_M_H_NICK_MA,07500第2行:K_G_M_H_NICK_MA,07500规范?该文件具有