Mohammad Fajar
Mohammad Fajar

Reputation: 1007

Inconsistent result of MATLAB regex

I have some string separated by pipe (and period) that I tried to split using MATLAB regex that is:

m = 'mari|bersenang|senang.jpg'; 

In the first step I split that string by pipe so at the end there is a string senang.jpg. In the second step, I split this string by period. But the result different from what I got in the first step. In the first step the output just a cell that contains string. But in second step, there is cell inside cell to wrap the outpout from regex function. I dont understand how this happen? Because if I split string senang.jpg directly (not from the output of regex function in the first step) the output just look normal.

This is my complete script:

clear all ;
clc;

m = 'mari|bersenang|senang.jpg'; 
hasil = regexp(m, '\|', 'split');
hasil %  result a cell 

test = hasil(end)
hasil = regexp(test, '\.', 'split'); 
hasil  % result a cell inside a cell 
hasil{1} % result a cell
hasil{1}{2} % get content of a cell 

test = 'senang.JPG'; 
hasil = regexp(test, '\.', 'split'); 
hasil % output just a cell

note: I using MATLAB 2009a

Upvotes: 0

Views: 50

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112699

Replace

test = hasil(end)

by

test = hasil{end}; %// note curly braces

That way test is a string, not a cell containing a string.

Upvotes: 3

Related Questions