No BSD License  

Highlights from
CoCoMac

image thumbnail
from CoCoMac by James Allen
Download CoCoMac.org cortical connectivity data, model it as a network, and simulate epileptic sprea

fc_calculate_fitline(time_plot, time_plot_axes, time_colour)
function [linearFit, fitLine, fitLineLabel] = fc_calculate_fitline(time_plot, time_plot_axes, time_colour)

% Function to calculate a first degree fit line of the data points
% described by 'time_plot', using only the data points limited by the value
% in the 'fitline x axis range' GUI edit box. The line is plotted in the supplied colour 
% (this will be the same colour as the time_plot line). Returned:
% 'linearFit' : Coefficients of polyfit calculation
% 'fitLine': Handle to plotted line
% 'fitLineLabels': Handle to line label

% Query the x limits of the fitline from the gui editbox
fitXLim = str2num(get(findobj('tag', 'fit_limit_editbox'), 'string'));

%Check its an integer
if rem(fitXLim, 1) ~= 0
    errordlg('Error - x limit for fit line must be an integer. Rounding limit to nearest integer.');
    fitXLim = round(fitXLim);
    set(findobj('tag', 'fit_limit_editbox'), 'string', num2str(fitXLim));
end

% Check the limit isn't larger than the nnumber of time steps in the plot
noOfTimeSteps = length(time_plot);
if fitXLim > noOfTimeSteps
    errordlg('Error - X limit for fit line has been set to a value greater than the simulated number of time steps. Setting limit to the maximum number of time steps.');
    fitXLim = noOfTimeSteps;
    set(findobj('tag', 'fit_limit_editbox'), 'string', num2str(noOfTimeSteps));
end

% Shorten time_plot to the size of fitXLim
time_plot = time_plot(1, 1:fitXLim);

% Create time steps array (to plot fit line against) the size of fitXLim)
timeStepsArray = [];
for timeStepCounter = 1:fitXLim
    timeStepsArray(end+1) = timeStepCounter;
end

linearFit = polyfit(timeStepsArray, time_plot, 1);
fit = linearFit(1,1) * timeStepsArray + linearFit(1,2);

axes(time_plot_axes);
fitLine = plot(timeStepsArray, fit, '--', 'Color', time_colour); 

%Put text on line at half-way its length
fitLineLabel = text(round(fitXLim / 2), fit(round(fitXLim / 2)), ['m: ' num2str(linearFit(1,1))], 'color', time_colour);

% Hide the fit line if the 'show fitline' push button is not pressed
fc_hide_fitline(fitLine, fitLineLabel);

% Save log entry
fc_saveLog(['Slope of fit line (using x = 1:', num2str(fitXLim), ') = ', num2str(linearFit(1,1))]);
fc_saveLog(['Y-intercept of fit line: ', num2str(linearFit(1,2))]);

return

Contact us at files@mathworks.com