I'm trying to create a histogram of the theta and phi orientation of collagen fibers. When I try to plot the histogram I get the following error message:
Too many output arguments.
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Button PrivateButtonPushedFcn.
Below you can see the code I'm using. I would appreciate your help with finding a solution to this.
classdef histogram < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
OrientationhistogramofphiButton matlab.ui.control.Button
OrientationhistogramofthetaButton matlab.ui.control.Button
DcollagenorientationofphiButton matlab.ui.control.Button
DcollagenorientationofthetaButton matlab.ui.control.Button
LoaddataButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
methods (Access = private)
function meanc(app)
shgim = mean(double(shgim));3;
calcfibangspeed(app,shgim,armd,filton)= [aS];
% find size of image
sz=size(shgim,1);
sz2=size(shgim,2);
% filters the data using a Gaussian kernel
cdata=(shgim)/max(max(shgim));
if filton
hf = fspecial('gaussian',[9 9],1.2);
im=imfilter((double(cdata)+.0000001),hf);
else
im=cdata;
end
% create coordinates of window
[xk yk]=meshgrid(1:armd*2+1,1:armd*2+1);
% determine angle of each coordinate relative to center pixels xk and yk
xk=xk-armd-1;
yk=yk-armd-1;
[ang,radd] = cart2pol(xk,-1*yk);
% put all angular data between 0 and pi
ang=ang+pi*(ang<0);
% weighting factor 1/radius
wei=1./((radd));
wei=wei.*(wei>0);
% initialize variables
C=zeros(sz,sz2);
S=zeros(sz,sz2);
% This set of loops is where vector summation occurs. For each iteration a
% single directional vector is added to a running total.
for i=-armd:-1
for j=-armd:0
% creates 3 copies of the image: the original and two copies shifted
% in opposite directions by i and j
fim = circshift(im,[i j]);
bim = circshift(im,[-i -j]);
tim(:,:,1)=fim;
tim(:,:,2)=im;
tim(:,:,3)=bim;
% the standard deviation of tim is computed in the 3rd dimension,
% which means we're measuring how intensity varies between each
% pixel and its neighbors at relative shifts of i,j and -i,-j.
% We weight the orientation vector by the max possible standard
% deviation and the measured standard deviation
dc=sqrt(1/3)-(std(tim,0,3));
if ((i~=0)|(j~=0))
% we sum the x and y components of each vector and mulitply it
% by our weighting factors dc and wei
% note that because these are axial data (0deg=180deg), we
% multiply the angle by 2 (later we divide by 2)
C=C+(cos(2*ang(armd+1+i,armd+1+j)).*dc.*wei(armd+1+i,armd+1+j));
S=S+(sin(2*ang(armd+1+i,armd+1+j)).*dc.*wei(armd+1+i,armd+1+j));
end
end
end
for i=-armd:0
for j=1:armd
% creates 3 copies of the image: the original and two copies shifted
% in opposite directions by i and j
fim = circshift(im,[i j]);
bim = circshift(im,[-i -j]);
tim(:,:,1)=fim;
tim(:,:,2)=im;
tim(:,:,3)=bim;
% the standard deviation of tim is computed in the 3rd dimension,
% which means we're measuring how intensity varies between each
% pixel and its neighbors at relative shifts of i,j and -i,-j.
% We weight the orientation vector by the max possible standard
% deviation and the measured standard deviation
dc=sqrt(1/3)-(std(tim,0,3));
if ((i~=0)|(j~=0))
% we sum the x and y components of each vector and mulitply it
% by our weighting factors dc and wei
% note that because these are axial data (0deg=180deg), we
% multiply the angle by 2 (later we divide by 2)
C=C+(cos(2*ang(armd+1+i,armd+1+j)).*dc.*wei(armd+1+i,armd+1+j));
S=S+(sin(2*ang(armd+1+i,armd+1+j)).*dc.*wei(armd+1+i,armd+1+j));
end
end
end
meanc = atan2(S,C)/2;
meanc=(meanc).*(meanc>=0)+(meanc+pi).*(meanc<0);
aS=meanc;
end
function calcfibang3D(app)
function [aSDE,pSDER]=calcfibang3D(app, shgim,armdx,armdz,filton,para)
% This function calculates the voxel-wise orientation. The output and input
% indices are explained in the main program
im = shgim;
im = double(im);
sz = size(im,1);
sz2 = size(im,2);
sz3 = size(im,3);
% Here starts the determination of theta
terimc = zeros(sz,sz2,3*sz3);
terimc(:,:,1:sz3) = im;
terimc(:,:,(sz3+1):(2*sz3)) = im;
terimc(:,:,(2*sz3+1):(3*sz3)) = im;
imc = zeros(sz,sz2,sz3);
for i = 1:sz3
imc(:,:,i) = mean(terimc(:,:,(sz3+i-armdz):(sz3+i+armdz)),3);
end
clear terimc
meanc = zeros(sz,sz2,sz3);
h = waitbar(0,'Please Wait');
ij = 0;
nij = sz3;
for i = 1:sz3
ij = ij+1;
waitbar(ij/nij)
meanc(:,:,i) = calcfibangspeed(app,imc(:,:,i),armdx,filton);
end
close(h)
clear imc
% Here starts the determination of beta
terimj = zeros(3*sz,sz2,sz3);
terimj(1:sz,:,:) = im;
terimj((sz+1):(2*sz),:,:) = im;
terimj((2*sz+1):(3*sz),:,:) = im;
imj = zeros(sz,sz2,sz3);
for i = 1:sz
imj(i,:,:) = mean(terimj((sz+i-armdz):(sz+i+armdz),:,:),1);
end
clear terimj
% To calculate the orientation in 2D manner
meanj = zeros(sz,sz2,sz3);
h = waitbar(0,'Please Wait')
ij = 0;
nij = sz;
for i = 1:sz
ij = ij+1;
waitbar(ij/nij)
meanj(i,:,:) = calcfibangspeed(squeeze(imj(i,:,:)),armdx,filton);
end
close(h)
clear imj
meanj = (pi/2-meanj).*(meanj<=pi/2)+(3*pi/2-meanj).*(meanj>pi/2);
% Here starts the determination of gamma
terimg = zeros(sz,3*sz2,sz3);
terimg(:,1:sz2,:) = im;
terimg(:,(sz2+1):(2*sz2),:) = im;
terimg(:,(2*sz2+1):(3*sz2),:) = im;
img = zeros(sz,sz2,sz3);
for i = 1:sz
img(:,i,:) = mean(terimg(:,(sz2+i-armdz):(sz2+i+armdz),:),2);
end
clear terimg
% To calculate the orientation in 2D manner
meang = zeros(sz,sz2,sz3);
h = waitbar(0,'Please Wait')
ij = 0;
nij = sz2;
for i = 1:sz2
ij = ij+1;
waitbar(ij/nij)
meang(:,i,:) = calcfibangspeed(app,squeeze(img(:,i,:)),armdx,filton);
end
close(h)
clear img
meang = (pi/2+meang).*(meang<=pi/2)+(meang-(pi/2)).*(meang>pi/2);
% Here to acquire the orientation of phi
meanp = atan(sqrt(1./(tan(meanj).^2)+1./(tan(meang).^2)));
meanplim = meanp;
for i = 1:sz
for j = 1:sz2
for k = 1:sz3
if meanj(i,j,k) <= pi/2
meanp(i,j,k) = meanp(i,j,k);
else
meanp(i,j,k) = pi-meanp(i,j,k);
end
end
end
end
% Here to acquire the real phi orientation regardless of Z resolution
meanpreal = (pi/2)-(atan(para*tan((pi/2)-meanplim)));
for i = 1:sz
for j = 1:sz2
for k = 1:sz3
if meanj(i,j,k) <= pi/2
meanpreal(i,j,k) = meanpreal(i,j,k);
else
meanpreal(i,j,k) = pi-meanpreal(i,j,k);
end
end
end
end
% Here prepares the final output
aSDE = meanc*180/pi;
pSDER = meanpreal*180/pi;
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: LoaddataButton
function LoaddataButtonPushed(app, event)
sz = 512 ;
sz2 = 512 ;
sz3 = 30 ; % 46; % modify 1, define sz, sz2, and sz3 according to the size of the 3D image
shgstack = zeros(sz,sz2,sz3);
for i = 1:sz3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%7 modify
% shgstack(:,:,i) = imread(['C:\Users\myy15\Desktop\human500um1um\humanskin250umz1um\noshift2\',num2str(i),'.tif']); % modify 2
shgstack(:,:,i) = imread(['/Users/kaitlynn/Desktop/Matlab/imaging/',num2str(i),'.tif']); % modify 2
% shgstack(:,:,i) = imread(['C:\Users\myy15\Desktop\human500um1um\stack2\',num2str(i),'.tif']); % modify 2
% 'shgstack' is the 3D image used for directional variance analysis.
% Define the path of the folder where the images are saved
end
end
% Button pushed function: DcollagenorientationofthetaButton
function DcollagenorientationofthetaButtonPushed(app, event)
end
% Button pushed function: OrientationhistogramofthetaButton
function OrientationhistogramofthetaButtonPushed(app, event)
aSDE = meanc(app)*180/pi;
histogram(app.UIAxes,aSDE, sz,sz2, sz3) ;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [297 240 300 185];
% Create LoaddataButton
app.LoaddataButton = uibutton(app.UIFigure, 'push');
app.LoaddataButton.ButtonPushedFcn = createCallbackFcn(app, @LoaddataButtonPushed, true);
app.LoaddataButton.Position = [52 424 100 22];
app.LoaddataButton.Text = 'Load data';
% Create DcollagenorientationofthetaButton
app.DcollagenorientationofthetaButton = uibutton(app.UIFigure, 'push');
app.DcollagenorientationofthetaButton.ButtonPushedFcn = createCallbackFcn(app, @DcollagenorientationofthetaButtonPushed, true);
app.DcollagenorientationofthetaButton.Position = [10 364 184 22];
app.DcollagenorientationofthetaButton.Text = '2D collagen orientation of theta';
% Create DcollagenorientationofphiButton
app.DcollagenorientationofphiButton = uibutton(app.UIFigure, 'push');
app.DcollagenorientationofphiButton.Position = [16 314 173 22];
app.DcollagenorientationofphiButton.Text = '2D collagen orientation of phi';
% Create OrientationhistogramofthetaButton
app.OrientationhistogramofthetaButton = uibutton(app.UIFigure, 'push');
app.OrientationhistogramofthetaButton.ButtonPushedFcn = createCallbackFcn(app, @OrientationhistogramofthetaButtonPushed, true);
app.OrientationhistogramofthetaButton.Position = [13 190 176 22];
app.OrientationhistogramofthetaButton.Text = 'Orientation histogram of theta';
% Create OrientationhistogramofphiButton
app.OrientationhistogramofphiButton = uibutton(app.UIFigure, 'push');
app.OrientationhistogramofphiButton.Position = [19 130 165 22];
app.OrientationhistogramofphiButton.Text = 'Orientation histogram of phi';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = histogram
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end