Question

I can run this in cmd window, but facing problem when write it in function.

checkIP.m

function feature1 = checkIP(data)

%data = importdata('DATA/URL/testing_URL')

domain_URL = regexp(data,'\w*://[^/]*','match','once');
IPs = regexp(domain_URL,'(?<=//)(?<=//)(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})','match','once');

feature1_data = (~cellfun(@isempty,IPs))
feature1(feature1_data~=0)=1;
feature1(feature1_data==0)=-1
end

all.m

data = 'http://123.456.789.123/stackoverflow.com/questions/23245757/create-and-call-function';
feature1 = checkIP(data)
Was it helpful?

Solution

To run cellfun the input obviously has to be a cell. The problem is that if data is a char array rather than a cell array of char arrays, regexp will not return a cell, at least not when called with the 'once' option. The easiest solution is simply to ensure that the input to the first regexp call is a cell array, if only one with a single cell.

Anyway, right before the first regexp, add:

if ischar(data),
    data = cellstr(data);
end

Also note that the second regexp should escape the dot (.):

IPs = regexp(domain_URL,'(?<=//)(?<=//)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})','match','once');

And out of curiosity, why the repeated look-behind ((?<=//))?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top