Within a function I've programmatically created an editable uitable with a plot. Whenever the cell values change, the plot updates. I'd like to set the new edited table as the output. My code so far:
function outputTable = begrenzung()
t = table(Drehzahl, Drehmoment, 'RowNames',{'Startpunkt' 'P1' 'P2' 'P3' 'Endpunkt'});
fig = uifigure;
fig.Position(3:4) = [822 360];
uit = uitable(fig,'ColumnEditable',true);
uit.Data = t;
uit.FontSize = 10;
uit.FontWeight = 'bold';
uit.ColumnEditable = true;
uit.Position(3) = 375;
ax = uiaxes(fig);
ax.Position(1) = 415;
ax.YLabel.String = 'Drehmoment [Nm]';
ax.XLabel.String = 'Drehzahl [rpm]';
x = t{:,1};
y = t{:,2};
area(ax,x,y);
ax.XLim = [0 45];
ax.YLim = [0 2000];
ax.Title.String = 'Feld der gefahrenen Punkte';
uit.CellEditCallback = #updatePlot;
function outputTable = updatePlot(src,event)
area(ax,uit.Data{:,1},uit.Data{:,2});
end
end
How can I save the updated uitable after each value-change?
I have found a solution, even though this might not be the elegant way:
function [outputTable] = begrenzung()
t = table(Drehzahl, Drehmoment,...
'RowNames',{'Startpunkt' 'P1' 'P2' 'P3' 'P4' 'Endpunkt'});
fig = uifigure;
fig.Position(3:4) = [822 360];
uit = uitable(fig);
uit.Data = t;
uit.FontSize = 10;
uit.FontWeight = 'bold';
uit.ColumnEditable = true;
uit.Position(3) = 375;
ax = uiaxes(fig);
ax.Position(1) = 415;
ax.YLabel.String = 'Drehmoment [Nm]';
ax.XLabel.String = 'Drehzahl [rpm]';
x = t{:,1};
y = t{:,2};
fill(ax,x,y,'c');
ax.XLim = [0 45];
ax.YLim = [0 2000];
ax.Title.String = 'Feld der gefahrenen Punkte';
uit.CellEditCallback = #updatePlot;
function [test] = updatePlot(src,event)
fill(ax,uit.Data{:,1},uit.Data{:,2},'c');
outputTable = uit.Data;
end
outputTable = uit.Data;
uiwait(fig);
end
this way, my Output is the changed table
Related
Below you can see the code for convolution of two continuous functions. There is a function called fx which I took as the square root of a Gaussian distribution. The convolution is calculated using 2 methods. In one of them I use the built-in function conv() and in the other I use the definition of the convolution. In mat_conv1 and cont_conv1 the function fx is convolved with itself using the built-in and explicit calculation methods respectively. In mat_conv2 and cont_conv2 the two functions mat_conv1 and cont_conv1 are convolved with themselves using their respective methods. You can see the code below.
clear all
clc
a = 0;
b = 1;
c1 = 0.1;
c2 = 0.2;
d1 = 0.3;
d2 = 0.4;
ymin = -2;
ymax = 2;
ynum = 400;
ylist = linspace(ymin,ymax,ynum);
conv_1 = mat_conv2(ylist,d1,d2,c1,c2,a,b);
conv_2 = cont_conv2(ylist,d1,d2,c1,c2,a,b);
hold on
plot(ylist,conv_1,'DisplayName','conv()');
plot(ylist,conv_2','DisplayName','continuous');
legend('-DynamicLegend');
function res = fx(x,a,b)
res = sqrt(normpdf(x,a,b));
end
function res = mat_conv1(y,c1,c2,a,b)
dy = y(2)-y(1);
gx_1 = fx(y/c1,a,b);
gx_2 = fx(y/c2,a,b);
res = (1/(c1*c2))*conv(gx_1,gx_2,'same')*dy;
end
function res = mat_conv2(y,d1,d2,c1,c2,a,b)
dy = y(2)-y(1);
hx_1 = mat_conv1(y/d1,c1,c2,a,b);
hx_2 = mat_conv1(y/d2,c1,c2,a,b);
res = (1/(d1*d2))*conv(hx_1,hx_2,'same')*dy;
end
function res = cont_conv1(y,c1,c2,a,b)
dy = y(2)-y(1);
xx = -2:0.01:2;
for i=1:length(y)
gx_1 = fx(xx/c1,a,b);
gx_2 = fx((y(i)-xx)/c2,a,b);
yy = gx_1.*gx_2;
yy = trapz(xx,yy);
yy = (1/(c1*c2))*yy;
res(i) = yy;
end
end
function res = cont_conv2(y,d1,d2,c1,c2,a,b)
dy = y(2)-y(1);
xx = -2:0.01:2;
for i=1:length(y)
hx_1 = cont_conv1(xx/d1,c1,c2,a,b);
hx_2 = cont_conv1((y(i)-xx)/d2,c1,c2,a,b);
yy = hx_1.*hx_2;
yy = trapz(xx,yy);
yy = (1/(d1*d2))*yy;
res(i) = yy;
end
end
As you can see from the figure below the results match pretty neatly.
Now I only want to consider the positive domain. In other words, I want to take the convolution in the interval [0,2]. So I change ymin to zero and inside the functions cont_conv1 and cont_conv2 I start the vector xx from zero. The code is:
clear all
clc
a = 0;
b = 1;
c1 = 0.1;
c2 = 0.2;
d1 = 0.3;
d2 = 0.4;
ymin = 0;
ymax = 2;
ynum = 400;
ylist = linspace(ymin,ymax,ynum);
conv_1 = mat_conv2(ylist,d1,d2,c1,c2,a,b);
conv_2 = cont_conv2(ylist,d1,d2,c1,c2,a,b);
hold on
plot(ylist,conv_1,'DisplayName','conv()');
plot(ylist,conv_2','DisplayName','continuous');
legend('-DynamicLegend');
function res = fx(x,a,b)
res = sqrt(normpdf(x,a,b));
end
function res = mat_conv1(y,c1,c2,a,b)
dy = y(2)-y(1);
gx_1 = fx(y/c1,a,b);
gx_2 = fx(y/c2,a,b);
res = (1/(c1*c2))*conv(gx_1,gx_2,'same')*dy;
end
function res = mat_conv2(y,d1,d2,c1,c2,a,b)
dy = y(2)-y(1);
hx_1 = mat_conv1(y/d1,c1,c2,a,b);
hx_2 = mat_conv1(y/d2,c1,c2,a,b);
res = (1/(d1*d2))*conv(hx_1,hx_2,'same')*dy;
end
function res = cont_conv1(y,c1,c2,a,b)
dy = y(2)-y(1);
xx = 0:0.01:2;
for i=1:length(y)
gx_1 = fx(xx/c1,a,b);
gx_2 = fx((y(i)-xx)/c2,a,b);
yy = gx_1.*gx_2;
yy = trapz(xx,yy);
yy = (1/(c1*c2))*yy;
res(i) = yy;
end
end
function res = cont_conv2(y,d1,d2,c1,c2,a,b)
dy = y(2)-y(1);
xx = 0:0.01:2;
for i=1:length(y)
hx_1 = cont_conv1(xx/d1,c1,c2,a,b);
hx_2 = cont_conv1((y(i)-xx)/d2,c1,c2,a,b);
yy = hx_1.*hx_2;
yy = trapz(xx,yy);
yy = (1/(d1*d2))*yy;
res(i) = yy;
end
end
The result is given below.
As you can see the results are completely different. What am I doing wrong? How can I get rid of this discrepancy and use conv() to convolve the two functions in the positive domain?
function Test()
a = 2;
b = 1;
c = 0.5;
q = 0.001;
r = 10;
function F = Useful(x) %calculates existing values for x with size 11
eq1 = (1*(0.903*x(2))^(-1))-(0.903*x(1));
eq2 = (1*(0.665*x(3))*(0.903*x(2))^(-1))-0.903*x(4);
eq3 = (1*(0.399*x(5))*(0.903*x(2)))-0.665*x(6);
eq4 = (1*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(7);
eq5 = (1*(0.399*x(5))*(0.903*x(2))^3)-1*x(8);
eq6 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2)))-1*x(9);
eq7 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(10);
eq8 = (1*(0.665*x(3))*(0.399*x(5)))-0.903*x(11);
eq9 = x(3)+x(4)+x(9)+x(10)+x(11)-a;
eq10 = x(5)+x(6)+x(7)+x(8)+x(9)+x(10)+x(11)-b;
eq11 = x(2)+x(6)+2*x(7)+3*x(8)+x(9)+2*x(10)-x(1)-x(4)-c;
F = [eq1;eq2;eq3;eq4;eq5;eq6;eq7;eq8; eq9; eq10; eq11];
end
Value(1,1) = 0;
for d = 2:100
x = fsolve(#Useful,x0,options); %Produces the x(1) to x(11) values
Value(1,d) = (x(3)+x(5))*d+Value(1,d-1); %Gives a new value after each iteration
a = a-x(3);
b = b-x(5);
c = c-x(2);
end
function Zdot = rhs(t,z) %z = (e1,e2,e3,e4,e5)
Zdot=zeros(5,1);
Zdot(1) = -1*z(1);
Zdot(2) = 1*z(1);
Zdot(3) = 1*z(1) - 1*z(2)*z(3);
Zdot(4) = 1*1*z(1) - Value(1,100)*H(z(3))*z(4)*z(4);
Zdot(5) = Value(1,100)*H(z(3))*(z(4));
end
function hill = H(x)
hill = q/(q+x^r);
end
[T,Y] = ode15s(#rhs, [0, 120], [1, 0, 1, 0, 0]); %Solve second function with values giving z(1) to z(5)
plot(T,Y(:,5))
end
I'm wondering, is it possible to pass on each Value obtained (Value (1), Value (2)... so on), into "function Zdot" or is only the final value possible to pass on? Essentially is this possible to implement:
function Zdot = rhs(t,z) %z = (e1,e2,e3,e4,e5)
Zdot=zeros(5,1);
Zdot(1) = -1*z(1);
Zdot(2) = 1*z(1);
Zdot(3) = 1*z(1) - 1*z(2)*z(3);
Zdot(4) = 1*1*z(1) - Value(1,d)*H(z(3))*z(4)*z(4);
Zdot(5) = Value(1,d)*H(z(3))*(z(4));
end
Any insights would be much appreciated and I would be extremely grateful. Thank you in advance!
in matlab while compiling to c code imcrop failed to run.
is there is any alternative method or any way to suppress?
I am using Matlab2016a. Below is code snippet:
if ~isempty(bbox);
bbox = bbox(1,1:4);
Ic = imcrop(im,bbox);
bboxeye = step(faceDetectorLeye, Ic);
end
Edit:
fullcode
function signal = detectFatigue(im)
% Create a detector object
faceDetector = vision.CascadeObjectDetector('ClassificationModel','FrontalFaceLBP','MinSize',[100 100]);
faceDetectorLeye = vision.CascadeObjectDetector('EyePairBig');
% Detect faces
bbox = step(faceDetector, im);
if ~isempty(bbox);
bbox = bbox(1,1:4);
Ic = imcrop(im,bbox);
bboxeye = step(faceDetectorLeye, Ic);
if ~isempty(bboxeye);
bboxeye = bboxeye(1,1:4);
Eeye = imcrop(Ic,bboxeye);
[~, nce, ~ ] = size(Eeye);
% Divide into two parts
Leye = Eeye(:,1:round(nce/2),:);
Reye = Eeye(:,round(nce/2+1):end,:);
% make grascale
Leye = rgb2gray(Leye);
Reye = rgb2gray(Reye);
Leye = filterim(Leye);
Reye = filterim(Reye);
lroi = [ 12 5 30 16 ];
rroi = [ 18 5 30 16 ];
Leyeroi = imcrop(Leye,lroi);
Reyeroi = imcrop(Reye,rroi);
[lc, ~ ] = findcircle(Leyeroi);
[rc, ~ ] = findcircle(Reyeroi);
if isempty(lc) && isempty(rc)
signal = 1;
else
signal = 0;
end
end
end
function Leye=filterim(Leye)
se = strel('disk',2,4);
% dilate graysacle
Leye = imdilate(Leye,se);
% erode grayscale
Leye = imerode(Leye,se);
Leye = imadjust(Leye);
Leye = imcomplement(Leye);
Leye = im2bw(Leye,0.95);
Leye = imresize(Leye,[30 60]);
function [c, r] = findcircle(Leyeroi)
[c ,r ] = imfindcircles(Leyeroi,[5 9],'ObjectPolarity','bright','Sensitivity',0.95);
[rm,id] = max(r);
r = rm;
c = c(id,:);
if ~isempty(c)
c(1) = c(1) + 15;
c(2) = c(2) + 7;
end
and to test the detectFatigue.m below is the function
function testdetectFatigue()
vid=webcam(1);
for loop=1:10
im=snapshot(vid);
detectFatigue(im);
end
So I have 3D filled line plots created in MATLAB using the following code:
for k = 1: P
for j = 1: L
X22 = linspace(0,b*1000,N+1);
Y22 = Yijk(j,:,k);
n = length(X22);
Z22 = contact_force(j,:,k);
Xp2 = zeros(2*n,1);
Yp2 = zeros(2*n,1);
Xp2(1:n) = X22;
Xp2(n+1:2*n) = X22(n:-1:1);
Yp2(1:n) = Y22;
Yp2(n+1:2*n) = Y22(n:-1:1);
Zp2(1:n) = 0;
Zp2(n+1:2*n) = Z22(n:-1:1);
figure(100+k);
hold on
fill3(Xp2,Yp2,Zp2,'c');
hold off
title(['Contact force at fraction '...
num2str(-1*((k-P)/P)) ' of base pitch'])
end
end
Which creates the following image :
I want to find what the total value for each blue line is, how would I do this?
Summing the area under the curve:
sumZ = 0;
for k = 1: P
for j = 1: L
X22 = linspace(0,b*1000,N+1);
Y22 = Yijk(j,:,k);
n = length(X22);
Z22 = contact_force(j,:,k);
Xp2 = zeros(2*n,1);
Yp2 = zeros(2*n,1);
Xp2(1:n) = X22;
Xp2(n+1:2*n) = X22(n:-1:1);
Yp2(1:n) = Y22;
Yp2(n+1:2*n) = Y22(n:-1:1);
Zp2(1:n) = 0;
Zp2(n+1:2*n) = Z22(n:-1:1);
sumZ = sumZ+Zp2;
figure(100+k);
hold on
fill3(Xp2,Yp2,Zp2,'c');
hold off
title(['Contact force at fraction '...
num2str(-1*((k-P)/P)) ' of base pitch'])
end
end
How can I use the degree symbol when writing a GUI in Matlab? I can use ^\circ when plotting but this does not work when writing a GUI.
This is my GUI so far:
directory = 'Data';
SiteName = 'AAA';
done = false;
spcFrac = 2; %number of spaces = char;
num2delim = 20;
defSlt = 'aTemp';
loader = 'on';
fclose all;
outputOptions = {'Air temperature','aTemp'};
defaults = {'',''};
% long names for outputs
names = {'latitude (deg N)','altitude (m)'};
lines = [4,5];
[~,b] = sortrows(outputOptions); vL = length(b);
outN = cell(vL,1);outA = outN;outUn = outN;
for k = 1:length(b)
outUn{k}= outputOptions{b(k),1};
outN{k} = outputOptions{b(k),1};
outA{k} = outputOptions{b(k),2};
end
indx = strcmp(defSlt,outA);
slt{1} = outN{indx};
txt = outA{indx};
outN(indx) = [];
close all
bckColor = [.85 .85 .85];
figDims = [150 150 400 300];
lM = 15;
tM = 20;
rM = 15;
bM = 15;
pnS = 12;
bgPw = 180;
bgPh = 120;
btnH = 25;
txtH = 20;
txtW = 40;
txtS = 5;
spc = 9;
radioW = 150;
radioH = 20;
bigPanels(1,:) = [lM figDims(4)-bgPh-tM bgPw bgPh];
bigPanels(2,:) = bigPanels(1,:); bigPanels(2,1) = bigPanels(2,1)+pnS+bgPw;
filePanel = [bigPanels(2,1)+bgPw+pnS*2 bM+pnS+btnH ...
figDims(3)-bgPw*2-lM-pnS*3-rM figDims(4)-tM-pnS-btnH-bM];
btns(1,:) = [lM bigPanels(1,2)-spc-btnH bgPw btnH];
btns(2,:) = [bigPanels(2,1) bigPanels(1,2)-spc-btnH bgPw btnH];
btns(3,:) = [filePanel(1)+filePanel(3)-bgPw bM bgPw btnH];
btns(4,:) = [filePanel(1) bM radioW radioH];
numTxtIn = numel(defaults);
rowsTxt = ceil(numTxtIn/2);
for p = 1:rowsTxt
txtBoxL(p,:) = [lM bM+(p-1)*(pnS+txtH) txtW txtH];
txtBoxR(p,:) = [lM+pnS+bgPw bM+(p-1)*(pnS+txtH) txtW txtH ];
diaL(p,:) = [lM+txtW+txtS -txtS+bM+(p-1)*(pnS+txtH) txtBoxR(p,1)-...
lM-txtW-pnS txtH ];
diaR(p,:) = [txtBoxR(p,1)+txtW+txtS -txtS+bM+(p-1)*(pnS+txtH) ...
txtBoxR(p,1)-lM-txtW-pnS txtH ];
end
S.fh = figure('units','pixels',...
'position',figDims,...
'menubar','none',...
'resize','off',...
'numbertitle','off',...
'name','Configuration File','Color',bckColor);
movegui(S.fh,'center')
outFrame=uipanel('Parent',S.fh,'BackgroundColor',bckColor,...
'Title','Output options','FontSize',10);
setpixelposition(outFrame,[lM-spc btns(1,2)-spc ...
bgPw+spc*2 bgPh+btnH+spc*3+pnS]);
slcFrame=uipanel('Parent',S.fh,'BackgroundColor',bckColor,...
'Title','Output selections','FontSize',10);
setpixelposition(slcFrame,[bigPanels(2,1)-spc btns(1,2)-spc ...
bgPw+spc*2 bgPh+btnH+spc*3+pnS]);
ParamFrame=uipanel('Parent',S.fh,'BackgroundColor',bckColor,...
'Title','User parameters','FontSize',10);
setpixelposition(ParamFrame,[btns(1,1)-spc ...
bM-spc bgPw*2+pnS+spc*2 btns(1,2)-2*spc-2]);
for lm = 1:rowsTxt
indr = rowsTxt-lm+1;
S.outT(1,indr) = uicontrol('style','edit',...
'units','pix',...
'position',txtBoxL(indr,:),...
'HorizontalAlign','center',...
'string','Remove',...
'fontsize',8,'String',defaults{lm,1},...
'callback',{#txt_call,lines(lm,1)},...
'BackgroundColor','w');
S.outT(2,indr) = uicontrol('style','edit',...
'units','pix',...
'position',txtBoxR(indr,:),...
'HorizontalAlign','center',...
'string','Remove',...
'fontsize',8,'String',defaults{lm,2},...
'callback',{#txt_call,lines(lm,2)}...
,'BackgroundColor','w');
S.outD(1,indr) = uicontrol('style','text',...
'units','pix',...
'position',diaL(indr,:),...
'HorizontalAlign','left',...
'string','Remove',...
'fontsize',8,'String',names{lm,1},'BackgroundColor',bckColor);
S.outD(2,indr) = uicontrol('style','text',...
'units','pix',...
'position',diaR(indr,:),...
'HorizontalAlign','left',...
'string','Remove',...
'fontsize',8,'String',names{lm,2},'BackgroundColor',bckColor);
end
Here I would like to change degN into the degree symbol ^\circ does not work.
Depending on the symbols you want to use, you can use either extended ascii or unicode symbols. So, for instance, ^\circ is Alt+0176 which gives you ° (numbers must be typed on keypad and the zero is necessary). µ is Alt+0181, etc. If that doesn't work, you can also programmatically use char(). So char(176) will give you the degree sign.
I should add that the Alt+0XXX construct is a MS Windows (it's worked as far back as 95 AFAIK) shortcut. I am certain-ish that there are similar keyboard shortcuts for MacOS and your favorite flavor of *nix. The char command works everywhere.
From your example code above, you just need to change the definition of names:
names = {'latitude (°N)','altitude (m)'};
or
names = {['latitude (' char(179) 'N)'],'altitude (m)'};