如何从数组中选择行,其中string包含范围内的数字:MATLAB

时间:2017-09-06 07:28:04

标签: arrays string matlab indexing

我有这个数组:

AB01    4   7
AB02    3   4
AB02    2   4
AB03    9   5
AB01    3   3
AB04    3   2
AB05    4   1
AB03    4   1
AB05    3   4
AB04    1   5

我有2个数字输入,用于确定最小值和最大值。例如,如果我设置第一个输入n1 = 2和第二个输入n2 = 4,则表示我希望值为AB02AB03AB04的行在第一列中,我将得到如下输出:

AB02    3   4
AB02    2   4
AB03    9   5
AB04    3   2
AB03    4   1
AB04    1   5

我不知道如何从AB02 - AB04创建范围值,因为它不是数字。我真的很感激你的帮助。

编辑:我正在尝试使用这个脚本,我知道我的逻辑索引不对,但我被卡住了。

col1 = 3;
fmt = repmat('%s',1,col1);
enter cofid = fopen('Document2.txt', 'rt');de here
filecolumn = textscan(fid, fmt, 'Delimiter', ' ');
fclose(fid);
FF = horzcat(filecolumn{:});

y1 = input('INPUT1: ')
y = sprintf('AB%.2d',y1)
z1 = input('INPUT2: ')
z = sprintf('AB%.2d',z1)
for o = y:z
   while o == 1 
      index = find(strcmp(FF,o))
   end
   ff = FF(index,:)
end

2 个答案:

答案 0 :(得分:1)

让我们从单元格数组中的字符串末尾提取数字,你可以使用正则表达式保持一般,但我会假设你总是在4位数的末尾有一个2位数字像你所示的字符串......

% Your data
M = {'AB01'  4   7
     'AB02'  3   4
     'AB02'  2   4
     'AB03'  9   5
     'AB01'  3   3
     'AB04'  3   2
     'AB05'  4   1
     'AB03'  4   1
     'AB05'  3   4
     'AB04'  1   5};
% Extracting numbers, using cellfun to operate on each element of the cell array
nums = cellfun(@(r)str2num(r(3:4)), M(:,1));
>> nums = [1
           2
           2
           % ... others ...
           4]

现在我们可以使用逻辑索引来访问您想要的行

n1 = 2; % lower bound
n2 = 4; % upper bound
% Create logical array, true where nums is in range [n1,n2], false otherwise    
idx = nums >= n1 & nums <= n2;

并检索M

的行
output = M(idx,:);
>> output = 
   {'AB02'  3  4
    'AB02'  2  4
    'AB03'  9  5
    'AB04'  3  2
    'AB03'  4  1
    'AB04'  1  5}

作为参考,没有输出的所有代码可能看起来像这样:

% Input values
n1 = 2; n2 = 4;
% Your data stored in cell array M, get numbers
nums = cellfun(@(r)str2num(r(3:4)), M(:,1));
% Get rows within range
output = M(nums >= n1 & nums <= n2, :);

答案 1 :(得分:0)

我建议您使用表而不是单元格数组,并使用带有TextType字符串的tableread来导入数据。使用字符串(从16b开始可用)提取值更简单一些。如果您处理大量数据,字符串也会更快。

function emf()
var ss = SpreadsheetApp.openById('####').getSheetByName('EMF');
{
  var label = GmailApp.getUserLabelByName("tkh_emf");
  var threads = label.getThreads();

  for (var i=0; i<threads.length; i++)
  {
    var messages = threads[i].getMessages();
    for (var j=0; j<messages.length; j++)
    {
      var fullname = messages[j].getPlainBody().split("Name*: ")[1].split("\n")[0]; // used for GMap column title
      var fname = fullname.split(" ")[0];
      var lname = fullname.split(" ")[1];
      var email = messages[j].getPlainBody().split("Email*: ")[1].split("\n")[0];
      var phone = messages[j].getPlainBody().split("Phone*: ")[1].split("\n")[0];
      var addr = messages[j].getPlainBody().split("Street Address*: ")[1].split("\n")[0];
      var city = messages[j].getPlainBody().split("City*: ")[1].split("\n")[0];
      var find = messages[j].getPlainBody().split("hear about us?*: ")[1].split("\n")[0];
      var referrer = messages[j].getPlainBody().split("Referrer Name: ")[1].split("\n")[0];
      var photo = messages[j].getPlainBody().split("(max size 2MB): ")[1].split("\n")[0];
      var site = messages[j].getSubject().split("Contact ")[1].split("from")[0];
      var date = messages[j].getDate();
      var work = messages[j].getPlainBody().split("Brief description of work requested*: ")[1].split("Visitor IP: ")[0];
      var ip = messages[j].getPlainBody().split("Visitor IP: ")[1].split("Follow @emfteam")[0];

      // var rowNum = lastRow + 1;
      ss.appendRow([fullname, fname, lname, date, work, photo, email, phone, addr, city, find, referrer, site, ip,'pending','pending']);      
    }
      // threads[i].removeLabel(label);
  }
}