Clear Filters
Clear Filters

How to pass categorical names into dot plot and plot categorical data in for loop?

5 views (last 30 days)
Hello!
I have a two variables for two subjects from an experiment: control and a stimulus variable and for both of these variables I get one number per stimulus and one number for the control per subject. I also have this control and stimulus changing over time. I am trying to make a scatter plot of the percent change where I plot the categorical time change
where I have tried the following:
subj_controls = {'subject1' 'subject2'}
subj_stimulus = {'subject1' 'subject2'}
time = {'40_to_50' '50_to_60'}
[labels, ~, xdata_idx] = unique(times);
[labels_controls, ~, xdata_idx_control] = unique(times);
for s = 1:length(subj_controls)
subj = subj_controls{s}
for i = 1:length(time)
time = times{i}
plot_length = 1:length(xdata_idx)*2;
ax.XTick = [plot_length]
hold on
scatter([i i+1], [percent_change_control(:,i) percent_change(:,i)]); hold off
set(gca, 'XTick', [unique(xdata_idx)], 'XTickLabels', labels); hold off
end
end
where the outcome looks as attached where the Subject 1 Control, Subject 1 Stimulus, Subject 2 Control, Subject 2 Stimulus are not in discrete areas if they are at the 40-50 time mark vs. the 50-60 time mark:
and I have the script working as follows, but I am moving to a larger dataset and do not want to be manually writing all the categorical variables for every subject:
scatter([1 2], [percent_change_control(:,1) percent_change(:,1)]
scatter([3 4], [percent_change_control(:,2) percent_change(:,2)]
ax = gca;
ax.XTick = [1,2,3,4];
ax.XTickLabels = {'Control 40-50', 'Stimulus 40-50', 'Control 50-60', 'Stimulus 50-60'}
here is an example of the image I aiming for:
Does anyone have any suggestions? Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jun 2022
subj_controls = categorical({'subject1' 'subject2'});
subj_stimulus = categorical({'subject1' 'subject2'});
time = categorical({'40_to_50' '50_to_60'});
for s = 1:length(subj_controls)
subj = subj_controls(s);
for i = 1:length(time)
time = times(i);
scatter(time, [percent_change_control(:,i) percent_change(:,i)]);
hold on
end
end
At the moment, I do not see why you are looping over subj_controls since you seem to be doing the same thing for every subj
  1 Comment
nines
nines on 13 Jun 2022
Edited: nines on 13 Jun 2022
hello!
I think I am almost there, but am getting the following error:
Error using scatter (line 120)
Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or
data inputs which can be converted to double.
My variables are as follows:
percent_change_control = 1x9 categorical
percent_change = 1x9 categorical
time = 1x1 categorical
i tried to debug what was going on and the code is below:
%first try
for i = 1:length(times)
time = times(i)
scatter(time, [percent_change_control(:,i) percent_change(:,i)])
end
%second try
for i = 1:length(times)
time = times(i)
scatter(time(:,i), [percent_change_control(:,i) percent_change(:,i)])
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!