Extract a matrix under a specific string from a text file using matlab

时间:2016-04-15 11:06:49

标签: matlab

example

My text file is like

string 1 12 13 14 16 11 41 25 26 32 25 26 27

string 2 23 15 26 28 12 15 19 17 35 65 84 12

string 3 and so on...

I want that if I ask for string 1 , it will give me the corresponging matrix under string 1 and the size of the matrix is also not known i.e. 12 13 14 16 11 41 25 26 32 25 26 27

will anyone tell me how to do this?

thanks

1 个答案:

答案 0 :(得分:0)

您应该执行以下操作:

1 - 将字符串转换为整数/矩阵

M1 = str2num(string1) - http://www.mathworks.com/help/matlab/ref/str2num.html
M2 = str2num(string2)

2 - 如果要查找相应的值,只需比较:

for i=1:length(M1)
     [~,M3(i)]=ismember(M1(i),M2)
end

M3会为您提供M2中匹配的索引。