Effacer les filtres
Effacer les filtres

how to color part of a rasterplot using a color triplets array

1 vue (au cours des 30 derniers jours)
Leonardo Mascelli
Leonardo Mascelli le 21 Nov 2022
Réponse apportée : DGM le 31 Août 2023
Hi,
i'm trying to color with two different colors a rasterplot using a binary array that represents the presence/absence of a stimulus.
the rasterplot is made this way:
% TIME_SERIES is a cell array with the times of each event in each cell,
% representing different electrodes
% STIM_TIMES is the stimulation binary signal which is shared between series
% both are passed to the algorithm via the arguments of a function
for i = 1:numel(time_series)
xdata = repmat(time_series{i}', 3, 1);
ydata = nan(size(xdata));
ydata(1, :) = i-1;
ydata(2, :) = i;
colors = [stim_times zeros(size(stim_times)) zeros(size(stim_times))];
plot(hndl, xdata, ydata, "color", colors)
end
Unrecognized function or variable 'time_series'.
the issue is (in my opinion) the size of the stim_times array but the error reported is
Error using plot
Invalid RGB triplet. Specify a three-element vector of values between 0 and 1.
  2 commentaires
DGM
DGM le 21 Nov 2022
If colors is intentionally supposed to be a (binarized?) color table, then no, plot() can't accept that. You might try using scatter() instead.
Robert Daly
Robert Daly le 13 Juin 2023
By "binary" do you mean "boolean" i.e. true or false?
If you want to make a raster style plot I suggest you should try
imagesc
or
pcolor
something like
figure
pcolor(xdata, ydata, colors)
shading flat

Connectez-vous pour commenter.

Réponses (2)

ag
ag le 30 Août 2023
Hi,
I understand that you are getting an error while using the "plot" function in your script.
The error is in the following line of code:
colors = [stim_times zeros(size(stim_times)) zeros(size(stim_times))];
The error is caused due to the values being passed to the "color" input parameter of the "plot" function. To resolve this issue, make sure that the values assigned to the "colors" variable are triplets with each value ranging from 0 to 1.
For example, you can plot a figure with blue lines by using the following code:
plot(hndl, xdata, ydata, "color", [0 0 1]);
For more detailed information, please refer to the following documentation page:
Hope this helps!

DGM
DGM le 31 Août 2023
There were multiple ways to solve this:
% this emulates the inputs as described
N = 10;
time_series = linspace(0,10,N);
time_series = num2cell(time_series);
stim_times = rand(1,N)>0.5;
% either do it in a loop, generating one tuple at a time
for i = 1:numel(time_series)
xdata = repmat(time_series{i}', 3, 1);
ydata = nan(size(xdata));
ydata(1, :) = i-1;
ydata(2, :) = i;
colors = [stim_times(i) 0 0]; % a color tuple, not a table
plot(xdata, ydata, 'color', colors,'linewidth',2); hold on
end
... or instead of generating each tuple one at a time, you can generate the color table as was attempted originally:
figure
% or generate a color table and then use it in a loop
CT = [stim_times(:) zeros(numel(stim_times),2)]; % a color table
for i = 1:numel(time_series)
xdata = repmat(time_series{i}', 3, 1);
ydata = nan(size(xdata));
ydata(1, :) = i-1;
ydata(2, :) = i;
plot(xdata, ydata, 'color', CT(i,:),'linewidth',2); hold on
end
... but the loop isn't necessary. the whole thing can be simplified:
figure
% assemble x,y data
xdata = repmat(cell2mat(time_series),[3 1]);
ydata = (1:numel(time_series)) - [1; 0; NaN];
% set colororder, plot
CT = [stim_times(:) zeros(numel(stim_times),2)]; % a color table
colororder(CT)
plot(xdata, ydata,'linewidth',2)
The question remains whether this barely-readable staircase of tiny line segments was actually the appropriate output. This could probably be done simpler yet with a scatter plot or something else.
figure
% assemble x,y data
xdata = cell2mat(time_series);
ydata = 1:numel(time_series);
% plot the data using a short color table
scatter(xdata,ydata,100,stim_times,'filled');
CT = [0 0 0; 1 0 0]; % one color for each class
colormap(CT)
... but OP never clarified, so we can guess all we want now.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by