Matlab GUI edit text to double - matlab

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')

Related

Function to compute Scallop Loss

I want to make a function that computes the value of the Scallop loss for a Rectangular, Hamming and Blackman window using the Scallop loss formula.
I have created a function but it only returns that answer 0, have I made an error?
function s_l = scallop loss(len)
window = rectwin(len);
num_total = 0;
den_total = 0;
for n = 0:len-1
A1 = exp(-((1i*(n)*pi)/len));
A2 = window(n+1)*A1;
num = abs(A2);
den = win(n+1);
num_total = num_total + num;
den_total = den_total + den:
end
result = 20*log(num_total/den_total);
s_l = result;
You have a maths problem:
abs(sum(A)) != sum(abs(A))
They are not the same!
Change your code to:
window = rectwin(len);
num_total = 0;
den_total = 0;
for n = 0:len-1
A1 = exp(-((1i*(n)*pi)/len));
A2 = window(n+1)*A1;
num = A2; % !!
den = abs(window(n+1)); % you also forgot the abs here
num_total = num_total + num;
den_total = den_total + den;
end
num_total=abs(num_total); % !!
result = 20*log(num_total/den_total);
s_l = result;
please change the log() function to log10(). without it the result is completely wrong :)
I mean this line:
result = 20*log10*(num_total/den_total);

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);

Import data and Call function

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

How to skip an error inside a loop and let the loop continue

The following is my full code: (Most of it isn't useful for what I'm asking, but I just put in the entire code for context, the part of the code that is causing me trouble is towards the end)
clc
clear
P = xlsread('b3.xlsx', 'P');
d = xlsread('b3.xlsx', 'd');
CM = xlsread('b3.xlsx', 'Cov');
Original_PD = P; %Store original PD
LM_rows = size(P,1)+1; %Expected LM rows
LM_columns = size(P,2); %Expected LM columns
LM_FINAL = zeros(LM_rows,LM_columns); %Dimensions of LM_FINAL
% Start of the outside loop
for k = 1:size(P,2)
P = Original_PD(:,k);
interval = cell(size(P,1)+2,1);
for i = 1:size(P,1)
interval{i,1} = NaN(size(P,1),2);
interval{i,1}(:,1) = -Inf;
interval{i,1}(:,2) = d;
interval{i,1}(i,1) = d(i,1);
interval{i,1}(i,2) = Inf;
end
interval{i+1,1} = [-Inf*ones(size(P,1),1) d];
interval{i+2,1} = [d Inf*ones(size(P,1),1)];
c = NaN(size(interval,1),1);
for i = 1:size(c,1)
c(i,1) = mvncdf(interval{i,1}(:,1),interval{i,1}(:,2),0,CM);
end
c0 = c(size(P,1)+1,1);
f = c(size(P,1)+2,1);
c = c(1:size(P,1),:);
b0 = exp(1);
b = exp(1)*P;
syms x;
eqn = f*x;
for i = 1:size(P,1)
eqn = eqn*(c0/c(i,1)*x + (b(i,1)-b0)/c(i,1));
end
eqn = c0*x^(size(P,1)+1) + eqn - b0*x^size(P,1);
x0 = solve(eqn);
for i = 1:size(x0)
id(i,1) = isreal(x0(i,1));
end
x0 = x0(id,:);
x0 = x0(x0 > 0,:);
clear x;
for i = 1:size(P,1)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
end
x = [x0'; x];
x = double(x);
x = x(:,sum(x <= 0,1) == 0)
lamda = -log(x);
LM_FINAL(:,k) = lamda;
end
% end of the outside loop
The important part of the above loop is towards the end:
x = x(:,sum(x <= 0,1) == 0)
This condition is sometimes not satisfied and hence the variable x is empty, which means LM_FINAL(:,k) = lamda is also empty. When this happens, I get the error:
x =
Empty matrix: 43-by-0
Improper assignment with rectangular empty matrix.
Error in Solution (line 75)
LM_FINAL(:,k) = lamda;
How can I skip this error so that the column for LM_FINAL remains as empty, but the loop continues (so that the rest of LM_FINAL's columns are filled) rather than terminating?
You can use try and catch phrase to explicitly handle errors inside loop (or elsewhere in your code).

Shorter code in matlab to minimize data and getting corresponding indices

EDIT How to simplify the following code:
if(x(a) > x(b))
s = b;
e = a;
else
s = a;
e = b;
end
I can get it shorter like:
s = a;
e = b;
if(x(a) > x(b))
s = b;
e = a;
end
Thanks!
EDIT
h = [a b];
[~, idx] = min([x(a) x(b)]);
s = h(idx)
e = h(3-idx)
Are you sure your code does what you wanted it to do?
Maybe you want to try
s = min([x(a),x(b)]);
e = max([x(a),x(b)]);
EDIT: OK there you go,
r = [find(x==max(x), find(x==minx)];
you have what you need in r but if you need them as s and e then:
s = r(1);
e = r(2);