How can I fix the sparse error for spike detection of calcium imaging data?

1 view (last 30 days)
I am new to matlab and I am using a code that's already written and provided in a published manuscript. The m.script is for detecting calcium spikes for a calcium transients experiment. But is giving me the following error:
%% Calculate the amplitude of all peaks above threshold
for j=1:num_record
spike_data = data(j,:);
sigma = std(data(j,:));
threshold = mean(data(j,:)) + 1.5 * sigma; %Adujust the threshold based on the variation of the data, should be mean + 1-2 *std
% threshold = 2 * sigma; % Second way of setting threshold, in this way the threshold is 2-4 * sigma
spike_index = spike_detection(spike_data, threshold);
amplitude = data(j, spike_index);
timestamp{j} = sparse(j,spike_index,amplitude);
amplitudes{j} = amplitude;
end
Error using sparse
sparse(i,j,s,...) does not accept char inputs s. Use sparse(i,j,double(s),...) instead.
Error in mmc6 (line 14)
timestamp{j} = sparse(j,spike_index,amplitude);
How can I fix this?

Answers (1)

Vaibhav
Vaibhav on 17 Nov 2023
Hi Yojet
I understand that you would like to fix the error during the amplitude calculation using the "sparse" function.
The error message suggests an attempt to create a sparse matrix with character (char) input for the value (s) parameter. The sparse function doesn't accept character inputs for the value parameter. To resolve this issue, possible workaround is to convert the amplitude values to double precision numbers before supplying them to the sparse function.
The following code snippet addresses the issue by converting the amplitude to double:
for j = 1:num_record
spike_data = data(j,:);
sigma = std(data(j,:));
threshold = mean(data(j,:)) + 1.5 * sigma;
spike_index = spike_detection(spike_data, threshold);
amplitude = double(data(j, spike_index)); % Convert amplitude to double
timestamp{j} = sparse(j, spike_index, amplitude);
amplitudes{j} = amplitude;
end
You can refer to the following MathWorks documentation link to know more about "sparse" function:
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!