Import data and Call function - matlab

How do I use the function "is_double_url" to check 700 data that saved in the file name "training_URL", I can only check the data in cmd window or type "is_double_url('www.google.com')" in f10.m. But i want to use the import data as shown below to call out the function and check the 700 data
is_double_url.m file
function out = is_double_url(url_path1)
if url_path1(end)~='/'
url_path1(end+1)='/';
end
url_path1 = regexprep(url_path1,'//','//www.');
url_path1 = regexprep(url_path1,'//www.www.','//www.');
f1 = strfind(url_path1,'www.');
if numel(f1)<2
out = false;
else
f2 = strfind(url_path1,'/');
f3 = bsxfun(#minus,f2,f1');
count_dots = zeros(size(f3,1),1);
for k = 1:size(f3,1)
[x,y] = find(f3(k,:)>0,1);
str2 = url_path1(f1(k):f2(y));
if ~isempty(strfind(str2,'..'))
continue
end
count_dots(k) = nnz(strfind(str2,'.'));
end
out = ~any(count_dots(2:end)<2);
if any(strfind(url_path1,'://')>f2(1))
out = true;
end
end
return;
f10.m file
url_path1 = importdata('DATA\URL\training_URL')
out = is_double_url(url_path1)

Note the change in the numel(.) condition:
function out = is_double_url(url_path1)
if url_path1(end)~='/'
url_path1(end+1)='/';
end
url_path1 = regexprep(url_path1,'//','//www.');
url_path1 = regexprep(url_path1,'//www.www.','//www.');
f1 = strfind(url_path1,'www.');
if numel(f1)>2 % changed it here
out = false;
else
f2 = strfind(url_path1,'/');
f3 = bsxfun(#minus,f2,f1');
count_dots = zeros(size(f3,1),1);
for k = 1:size(f3,1)
[x,y] = find(f3(k,:)>0,1);
str2 = url_path1(f1(k):f2(y));
if ~isempty(strfind(str2,'..'))
continue
end
count_dots(k) = nnz(strfind(str2,'.'));
end
out = ~any(count_dots(2:end)<2);
if any(strfind(url_path1,'://')>f2(1))
out = true;
end
end
if ~out % I'm not sure if this is what you want..
out=-1;
end
return;
Regarding the issue you mentioned in comments, let's say url_path1 is a cell array of size 700x1; then (I think) you could say
out=[];
for i=1:size(url_path1,1)
out=cat(2,out,is_double_url(url_path1{i,1}));
end

Related

printing the content of a cell in matlab into a file

I have a cell datastructure and I am trying to print it to a file I keep getting this error :
Error using fprintf
Function is not defined for 'cell' inputs.
solWT = optimizeCbModel(recon1,'max','one');
fileID = fopen('Q3.txt','w');
grRateWT = solWT.f
C=cell(2,2);
for system = 1:2%length(recon1.subSystems)
system
fluxReactions = recon1.rxns(ismember(recon1.subSystems,recon1.subSystems(system)));
for reaction = 1:length(fluxReactions)
model = recon1;
model.lb(reaction) = 0;
model.ub(reaction) = 0;
end
solKO = optimizeCbModel(model,'max');
C{system, 1} = recon1.subSystems(system);
C{system, 2} = solKO.f / grRateWT;
end
sortedCellArray = sortrows(C,2);
formatSpec = '%s %2.1f\n';
[nrows,ncols] = size(C);
for row = 1:nrows
fprintf(fileID,formatSpec,sortedCellArray{row,:});
end
What am I doing wrong
Try
fprintf(fileID,formatSpec,sortedCellArray{row,1}, sortedCellArray{row,2})

writing function with two outputs

I have written the following code. I would like the function to return the matrices D & S but at the moment it simply returns the matrix 'ans' which is equal to S. Any suggestions would be appreciated. Thanks.
function [D,S] = sdQRS(QRS_cell)
%scales & dilates QRS complexes
m = length(QRS_cell{1}(:,1));
n = length(QRS_cell);
d1 = linspace(0.5,1.0,10);
d2 = linspace(1.0,2.0,21);
d = vertcat(d1',(d2(2:21))');
s1 = linspace(0.6,1.0,13);
s2 = linspace(1.0,1.5,18);
s = vertcat(s1',(s2(2:18))');
DIL = cell(n,1);
SCAL = cell(n,1);
for i = 1:n
DIL{i} = zeros(m,length(d));
SCAL{i} = zeros(m,length(d));
for j = 1:length(d)
DIL{i}(:,j) = interp1(QRS_cell{i}(:,1),QRS_cell{i}(:,2),QRS_cell{i}(:,1)/d(j),'linear','extrap');
SCAL{i}(:,j) = s(j)*QRS_cell{i}(:,2);
end
end
D = zeros(n);
S = zeros(n);
for k = 1:n
for l = 1:n
e = [];
t = [];
for a = 1:length(d)
e(a) = euc_dilQ(QRS_cell{k},QRS_cell{l},d(a));
t(a) = euc_scalQ(QRS_cell{k},QRS_cell{l},s(a));
end
[M,E] = min(e);
[M,T] = min(t);
D(k,l) = d(E);
S(k,l) = s(T);
end
end
You can specify a Matlab function to return any number of output values. In your case the function signature would look something like
function [D,S] = sdQRS(QRS_cell) {
d1 = linspace(0.5,1.0,10);
...
}
Now you can call that function by entering
[D,S] = sdQRS(QRS_cell);

Matlab GUI edit text to double

Can anybody tell me what's wrong with that code?
c = str2double(get(handles.edit6,'String'));
Or how else can I get the edit input to be the value of c?
The command window shows: 'Warning: Infinite or Not-a-Number value encountered. '
(input of edit6 is '0')
Code of the hole function:
function pushbutton1_Callback(hObject, eventdata, handles)
kpr = get(handles.slider1,'Value');
kir = get(handles.slider2,'Value');
kdr = get(handles.slider3,'Value');
e = handles.e;
a = 0;
b = 0;
c = str2double(get(handles.edit6,'Value'));
for t = handles.time;
ealt = e;
if t<handles.sprung
e = 0;
else
e = (a*t^2)+(b*t)+ c;
end
B(1,handles.ix) = e;
dedt = (e -ealt)/handles.h;
y = e*kpr + kir*(integral(#(x) 0*x+e,0,t-handles.sprung)) + kdr*dedt;
A(1,handles.ix) = y;
handles.ix = handles.ix+1;
hold on
plot(t,e)
end
handles.A = A;
plot(handles.time,handles.A,'r')

Parfor: Not enough input arguments

I have found a weird behavior of the parfor functionality in matlab. Have a look at the following code:
function [x,y,z] = parForTest(test3)
x = zeros(100,1);
y = zeros(100,1);
z = zeros(100,1);
test1 = ones(1,100);
test2 = zeros(1,100);
useSameTest2 = false;
if nargin == 1
test3Given = true;
else
test3Given = false;
end
parfor s = 1:numel(test1)
if useSameTest2
tmpTest = test2(1);
if test3Given, tmpTest3 = test3(1); end
else
tmpTest = test2(s);
if test3Given, tmpTest3 = test3(s); end
end
if test3Given
[xt, yt, zt] = oneCombi(test1(s), tmpTest, tmpTest3);
else
[xt, yt, zt] = oneCombi(test1(s), tmpTest);
end
%% store results
x(s) = xt;
y(s) = yt;
z(s) = zt;
end
end
function [xt, yt, zt] = oneCombi(t1, tmpTest, tmpTest3)
if nargin == 3
xt = 1;
yt = 1;
zt = 1;
else
xt = 0;
yt = 0;
zt = 0;
end
end
If you call this function by typing "[x,y,z] = parForTest();" you will get an error saying that there are not enough input arguments. If you run the whole thing with "for" instead of "parfor" it will work. It will also work, when you call "[x,y,z] = parForTest(ones(100,1));" Can somebody please explain me this behavior? It seems to be connected to the fact that I have an optional argument. If I add the line "test3 = [];" after "test3Given = false;" it will also work.
Matlab2014a with parallel computing toolbox.
You are using the optional input functionality incorrectly. I have noticed that matlab sometimes allows a call of the type F() even if the function is declared as function result = F(x, y). This call will be allowed if during the execution the variables x and y are not used. I know it is weird.
Now, your problem is related to the fact that you are using the parallel computing toolbox. When using parfor, Matlab is more stringent with the variables used inside the parfor-loop, that is why it works happily with the for-loop but not with the parfor. Matlab needs to ensure that all the workers receive the correct information, properly sliced, and properly defined (regardless of the execution path).
To solve your issue you need to define your input argument as varargin and then act on the different inputs with nargin. You need to modify your function as follows (see how the variable test3 is always defined, no matter if you call it as parForTest() or parForTest(whatever)):
function [x,y,z] = parForTest(varargin)
x = zeros(100,1);
y = zeros(100,1);
z = zeros(100,1);
test1 = ones(1,100);
test2 = zeros(1,100);
useSameTest2 = false;
if nargin == 1
test3Given = true;
test3 = varargin{1};
else
test3Given = false;
test3 = [];
end
parfor s = 1:numel(test1)
if useSameTest2
tmpTest = test2(1);
if test3Given, tmpTest3 = test3(1); end
else
tmpTest = test2(s);
if test3Given, tmpTest3 = test3(s); end
end
if test3Given
[xt, yt, zt] = oneCombi(test1(s), tmpTest, tmpTest3);
else
[xt, yt, zt] = oneCombi(test1(s), tmpTest);
end
% store results
x(s) = xt;
y(s) = yt;
z(s) = zt;
end

Binary file - Matrix Manipulation

I have several binary files with a known structure (8,12000,real*4). 8 is the number of variables, and 12000 represent the time steps. From these binaries I need to get a final 16x9 matrix defined as followed:
the first column contains a filename identification.
on the diagonal are located the extreme values (maxima and minima) of the corresponding variable.
the simultaneous values of the other variables shall be given in the rows.
At the moment I'm using this code
for i = 1:num_files
for j = 1:num_ext
fid = fopen([fullfile(BEGINPATH{1},FILELIST{i}) '.$' VAREXTENSION{j}],'r');
A = fread(fid,[DLC_dimens{i}{3}(1) DLC_dimens{i}{3}(2)],'real*4');
for k = 1:size(A,1)
[max_val(k,i) max_idx(k,i)] = max(A(k,:));
[min_val(k,i) min_idx(k,i)] = min(A(k,:));
end
fclose(fid);
end
end
% Pre-allocate ULS matrices
uloads = cell(2*size(A,1),size(A,1)+1);
uloads_temp = cell(2*size(A,1),size(A,1)+1);
% Generate ULS matrix for the first file
for i = 1:size(uloads,1)
uloads{i,end} = DLC_dimens{1}(1);
end
fid = fopen([fullfile(BEGINPATH{1},FILELIST{1}) '.$' VAREXTENSION{1}],'r');
A = fread(fid,[DLC_dimens{i}{3}(1) DLC_dimens{i}{3}(2)],'real*4');
for j = 1:size(uloads,2)-1
uloads{1,j} = A(j,max_idx(1,1))*DLC_dimens{1}{4};
uloads{2,j} = A(j,min_idx(1,1))*DLC_dimens{1}{4};
uloads{3,j} = A(j,max_idx(2,1))*DLC_dimens{1}{4};
uloads{4,j} = A(j,min_idx(2,1))*DLC_dimens{1}{4};
uloads{5,j} = A(j,max_idx(3,1))*DLC_dimens{1}{4};
uloads{6,j} = A(j,min_idx(3,1))*DLC_dimens{1}{4};
uloads{7,j} = A(j,max_idx(4,1))*DLC_dimens{1}{4};
uloads{8,j} = A(j,min_idx(4,1))*DLC_dimens{1}{4};
uloads{9,j} = A(j,max_idx(5,1))*DLC_dimens{1}{4};
uloads{10,j} = A(j,min_idx(5,1))*DLC_dimens{1}{4};
uloads{11,j} = A(j,max_idx(6,1))*DLC_dimens{1}{4};
uloads{12,j} = A(j,min_idx(6,1))*DLC_dimens{1}{4};
uloads{13,j} = A(j,max_idx(7,1))*DLC_dimens{1}{4};
uloads{14,j} = A(j,min_idx(7,1))*DLC_dimens{1}{4};
uloads{15,j} = A(j,max_idx(8,1))*DLC_dimens{1}{4};
uloads{16,j} = A(j,min_idx(8,1))*DLC_dimens{1}{4};
end
fclose(fid);
% ULS temporary matrix generation
uls = uloads;
for i = 2:num_files
fid = fopen([fullfile(BEGINPATH{1},FILELIST{i}) '.$' VAREXTENSION{1}],'r');
A = fread(fid,[8 12000],'float32');
for j = 1:size(uloads,1)
uloads_temp{j,9} = DLC_dimens{i}(1);
end
for k = 1:size(uloads,2)-1
uloads_temp{1,k} = A(k,max_idx(1,i))*DLC_dimens{i}{4};
uloads_temp{2,k} = A(k,min_idx(1,i))*DLC_dimens{i}{4};
uloads_temp{3,k} = A(k,max_idx(2,i))*DLC_dimens{i}{4};
uloads_temp{4,k} = A(k,min_idx(2,i))*DLC_dimens{i}{4};
uloads_temp{5,k} = A(k,max_idx(3,i))*DLC_dimens{i}{4};
uloads_temp{6,k} = A(k,min_idx(3,i))*DLC_dimens{i}{4};
uloads_temp{7,k} = A(k,max_idx(4,i))*DLC_dimens{i}{4};
uloads_temp{8,k} = A(k,min_idx(4,i))*DLC_dimens{i}{4};
uloads_temp{9,k} = A(k,max_idx(5,i))*DLC_dimens{i}{4};
uloads_temp{10,k} = A(k,min_idx(5,i))*DLC_dimens{i}{4};
uloads_temp{11,k} = A(k,max_idx(6,i))*DLC_dimens{i}{4};
uloads_temp{12,k} = A(k,min_idx(6,i))*DLC_dimens{i}{4};
uloads_temp{13,k} = A(k,max_idx(7,i))*DLC_dimens{i}{4};
uloads_temp{14,k} = A(k,min_idx(7,i))*DLC_dimens{i}{4};
uloads_temp{15,k} = A(k,max_idx(8,i))*DLC_dimens{i}{4};
uloads_temp{16,k} = A(k,min_idx(8,i))*DLC_dimens{i}{4};
end
if uloads_temp{1,1}(:) > uls{1,1}(:)
uls(1,:) = uloads_temp(1,:);
end
if uloads_temp{2,1}(:) < uls{2,1}(:)
uls(2,:) = uloads_temp(2,:);
end
if uloads_temp{3,2}(:) > uls{3,2}(:)
uls(3,:) = uloads_temp(3,:);
end
if uloads_temp{4,2}(:) < uls{4,2}(:)
uls(4,:) = uloads_temp(4,:);
end
if uloads_temp{5,3}(:) > uls{5,3}(:)
uls(5,:) = uloads_temp(5,:);
end
if uloads_temp{6,3}(:) < uls{6,3}(:)
uls(6,:) = uloads_temp(6,:);
end
if uloads_temp{7,4}(:) > uls{7,4}(:)
uls(7,:) = uloads_temp(7,:);
end
if uloads_temp{8,4}(:) < uls{8,4}(:)
uls(8,:) = uloads_temp(8,:);
end
if uloads_temp{9,5}(:) > uls{9,5}(:)
uls(9,:) = uloads_temp(9,:);
end
if uloads_temp{10,5}(:) < uls{10,5}(:)
uls(10,:) = uloads_temp(10,:);
end
if uloads_temp{11,6}(:) > uls{11,6}(:)
uls(11,:) = uloads_temp(11,:);
end
if uloads_temp{12,6}(:) < uls{12,6}(:)
uls(12,:) = uloads_temp(12,:);
end
if uloads_temp{13,7}(:) > uls{13,7}(:)
uls(13,:) = uloads_temp(3,:);
end
if uloads_temp{14,7}(:) < uls{14,7}(:)
uls(14,:) = uloads_temp(14,:);
end
if uloads_temp{15,8}(:) > uls{15,8}(:)
uls(15,:) = uloads_temp(15,:);
end
if uloads_temp{16,8}(:) < uls{16,8}(:)
uls(16,:) = uloads_temp(16,:);
end
fclose(fid);
end
Now comes the question: I was thinking of a procedure where
I generate a temporary uloads_temp matrix just with the first file;
Calculate the uloads matrix for the i-th file (i = 2:num_files)
Compare the terms on the diagonal between i-th uloads matrix and temporary uloads_temp:
a) if the elements of the i-th ulaods are major (minor) than the respective uloads_temp values
b) update uloads_temp rows the condition a) occurs.
I hope I explained everything properly. Could you please give me a hint on how to perform the described loops?
I thank you all in advance.
WKR,
Francesco
P.S. : everything could be reproduced by means fo matrices filled of random numbers; I just copied and pasted my code with reference to a file list.