Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB’s extractBetween alternative for Octave in cell array

I am trying to run a MATLAB code in Octave. There is a MATLAB function extractBetween that is not yet available in octave. Original code is

numbers = str2double(extractBetween(dirAndFileNames(:,2), 4, 5));

I have tried to replace it with this code.

numbers = str2double(substr(dirAndFileNames(:,2), 4, 2));

It works for single strings but not for cell arrays. I have tried other functions that work with cell arrays like strtrim, strtrunc. I want to extract the numbers on the end but strtrunc gives opposite of what I want. It gives the first letters.

dirAndFilenames(:,2) looks like this:

debug> dirAndFileNames(:,2)
ans =
{
  [1,1] = desktop.ini
  [2,1] = trn01
  [3,1] = trn02
  [4,1] = trn03
  [5,1] = trn04
  [6,1] = trn05
  [7,1] = trn06
  [8,1] = trn07
  [9,1] = trn08
  [10,1] = trn09
  [11,1] = trn10
}
like image 341
neogeomat Avatar asked Nov 25 '25 13:11

neogeomat


2 Answers

As @cris suggested, i had to loop over.

for i = 1:length(dirAndFileNames) 
    numbers{i} = str2double(substr(dirAndFileNames(i,2){1}, 4, 2)) 
end
like image 146
neogeomat Avatar answered Nov 28 '25 06:11

neogeomat


You can use cellindexmat :

a = dirAndFileNames(:, 2);
result = cellindexmat(a, 4:5);
like image 24
rahnema1 Avatar answered Nov 28 '25 06:11

rahnema1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!