简体   繁体   English

如何读取在Matlab中的字符串变量中出现的预定字符的编号

[英]How to read the number of a prespecified character that appears in a string variable in Matlab

I process a big file with Matlab. 我用Matlab处理一个大文件。 In each line of the input file, data are separated with dots " . ". 在输入文件的每一行中,数据用点“ . ”分隔。 Due to poor format, the number of dots may change line by line of the input file. 由于格式不正确,点数可能会在输入文件的一行中逐行更改。

For example: 例如:

line1 = 'DIDYMOTE.150.L20'
line2 = 'N.ELBETI.150.L10'

How can I read the number of dots that appear in each line ? 如何读取每行中出现的点数?

In matlab everything is an array. 在matlab中,所有内容都是一个数组。 So 所以

data = load('file.txt');
[no_lines, no_characters] = size(data);

for i = 1 : no_lines
    no_dots[i] = 0
    for j = 1 : no_characters
         if data[i][j] == '.'
             no_dots[i] = no_dots[i] + 1
         end
    end
end

However, matlab has no strings, and is very unsuitable for handling text data. 但是,matlab没有字符串,非常不适合处理文本数据。 If any of the lines has different length you will get an error. 如果任何行的长度不同,您将得到一个错误。 Even if this is not the case, you are better off using another language for this. 即使不是这种情况,最好还是使用其他语言。 It will take you less time to learn how to process text in Python (for example), than trying to fit your problem into matlab. 与尝试使问题适合Matlab相比,花更少的时间(例如)来学习如何在Python中处理文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM