Alicia
Alicia

Reputation: 13

Creating a FOR loop on Matlab to add samples to scatter3

I have a dataset with 41 rows and 100 columns. Each row represents a single sample with a known cell concentration (x) and viability (y) with 100 data measurements (c) measured across a frequency sweep (z). My end goal is to create a surface plot of all the data. Currently, I am trying to plot the data on a scatterplot and then will later interpolate the points and create a surface. At this stage, my problem comes from the scatterplot.

I can plot a single row using scatter3 without issue, but when I use a "for" loop to plot all the samples, it becomes a 2D scatterplot with strange colors.

How can I plot all the samples using a "for" loop?

Included below is an example of my code, using randomly generated values that are organized similarly to my dataset. I also added images of generated figures using a single row to show one sample and then using the for loop to add all samples to the scatterplot.

%Example 
x= randi(100,1,41); %concentration
x= transpose (x);
x= repmat (x,1, 100);

y= randi(100,1,41); %viability
y= transpose (y);
y= repmat (y,1, 100);

z= linspace(1,100,100); %frequency
z = repmat (z, 41, 1);

c= randi(100, 41,100); %measured data

%Practice plotting a single row (one sample)
figure (1)
scatter3(x(1,:),y(1,:),z(1,:),30,c(1,:),'filled');
colormap(turbo)                                
colorbar
xlabel('x')
ylabel('y')
zlabel('z')
title('Single Sample from one row')

% Plot all samples using a FOR loop
for j = 1:length(x)
    figure(2)
    hold on
    scatter3(x(j,:),y(j,:),z(j,:),30,c(j,:),'filled');
    colormap(turbo)                                
    colorbar
    xlabel('x')
    ylabel('y')
    zlabel('z')
    j=j+1;

end

Using the for loop on all samples

Example with one sample

Upvotes: 0

Views: 49

Answers (0)

Related Questions