How do I keep my current group IDs but then add in two other variables that the code needs to consider?

1 view (last 30 days)
I have a code below that seperates the items in an excel sheet into groups based on their lithology. I have attached it below along with the excel sheet. However, I am now needing to not only group by lithology but also to be able to read off each lithology's void ratio and bulk density. These two extra variables are also in the excel sheet, just in a different column. Is there any way to do this? Any help would be appriciated.
t = readtable('CostaRica_1253.xlsx');
[G,group_ID] = findgroups(t{:,4});
n_groups = numel(group_ID);
new_t = cell(1,n_groups);
for ii = 1:n_groups
new_t{ii} = t(G == ii,:);
end
Chalk1_1253=new_t{1};
%%
Gabbro_1253=new_t{2};
%%
Chalk2_1253=new_t{3};
%%
Chalk_1253=[Chalk1_1253;Chalk2_1253];
%%
Siliciclastic_1253=[Chalk_1253;Gabbro_1253];
Siliciclastic_1253_data = [table2array(Siliciclastic_1253(:,2)) table2array(Siliciclastic_1253(:,5:8))];
%%
figure(4)
plot(Siliciclastic_1253_data(:,3),Siliciclastic_1253_data(:,1),'b.');
axis ij
xlabel ('Porosity(%)');
ylabel ('Depth(m)');
title('Costa Rica 1253 Porosity Vs Depth');
hold on

Accepted Answer

Voss
Voss on 30 Dec 2022
Edited: Voss on 30 Dec 2022
t = readtable('CostaRica_1253.xlsx','VariableNamingRule','preserve');
[G,group_ID] = findgroups(t{:,4});
n_groups = numel(group_ID);
new_t = cell(1,n_groups);
for ii = 1:n_groups
new_t{ii} = t(G == ii,:);
end
% rather than hard-coding indices (e.g., Chalk_1253=[new_t{1};new_t{3}]),
% it's better to use the group_ID, in case you ever want to run this code
% on another input file where the groups are in a different order.
% Chalk_1253 = vertcat(new_t{strcmpi(group_ID,'chalk')});
% in this case, Siliciclastic_1253 includes all 3 groups, but I assume this
% input file is only part of the actual larger input file which has more
% groups (but if you really want to use all groups, you could just use the
% original table "t" instead of defining "Siliciclastic_1253" at all)
Siliciclastic_1253 = vertcat(new_t{ismember(lower(group_ID),{'chalk' 'gabbro'})});
% rather than using table2array(), you can construct a matrix from columns
% of the table like this:
% Siliciclastic_1253_data = [Siliciclastic_1253{:,[2 5:8]}];
% but you probably don't need that, since you can plot (or whatever) from
% the table directly:
figure
subplot(1,3,1)
plot(Siliciclastic_1253{:,6},Siliciclastic_1253{:,2},'b.');
axis ij
xlabel ('Porosity');
ylabel ('Depth(m)');
subplot(1,3,2)
plot(Siliciclastic_1253{:,7},Siliciclastic_1253{:,2},'b.');
axis ij
xlabel ('Void Ratio');
title('Costa Rica 1253');
subplot(1,3,3)
plot(Siliciclastic_1253{:,5},Siliciclastic_1253{:,2},'b.');
axis ij
xlabel ('Bulk Density(g/cm^3)');
% However, it seems likely that you want the plots to depict the different
% groups, and that the "Chalk" and "chalk" groups should be treated as one
% (so you can use lower(t{:,4}) in findgroups)
[G,group_ID] = findgroups(lower(t{:,4}));
n_groups = numel(group_ID);
new_t = cell(1,n_groups);
for ii = 1:n_groups
new_t{ii} = t(G == ii,:);
end
% create figure
figure
% pick some colors (make sure you have enough - one for each group):
colors = get(gca(),'ColorOrder');
% then plot the data for each group separately (or use gscatter)
for ii = 1:n_groups
subplot(1,3,1)
plot(new_t{ii}{:,6},new_t{ii}{:,2},'Color',colors(ii,:),'Marker','.','LineStyle','none');
hold on
subplot(1,3,2)
plot(new_t{ii}{:,7},new_t{ii}{:,2},'Color',colors(ii,:),'Marker','.','LineStyle','none');
hold on
subplot(1,3,3)
plot(new_t{ii}{:,5},new_t{ii}{:,2},'Color',colors(ii,:),'Marker','.','LineStyle','none');
hold on
end
subplot(1,3,1)
axis ij
xlabel ('Porosity');
ylabel ('Depth(m)');
subplot(1,3,2)
axis ij
xlabel ('Void Ratio');
title('Costa Rica 1253');
subplot(1,3,3)
axis ij
xlabel ('Bulk Density(g/cm^3)');
group_ID_for_legend = group_ID;
for ii = 1:n_groups
group_ID_for_legend{ii}(1) = upper(group_ID_for_legend{ii}(1));
end
legend(group_ID_for_legend,'Location','Best')
  2 Comments
Jacob Allen
Jacob Allen on 31 Dec 2022
Thank you so much! This definitly helps alot. If you wouldn't mind helping me further though, I have some more complex code that needs to function exactly how the code above functions. Im just not too sure on how to do it. Any help/tips would be appriciated.
t = readtable('Alaska_1418.xlsx');
[G,group_ID] = findgroups(append(t{:,3},' ',t{:,4}));
% make a cell array of tables, one for each group:
n_groups = numel(group_ID);
new_t = cell(1,n_groups);
for ii = 1:n_groups
new_t{ii} = t(G == ii,:);
end
% look at the table for group 1:
Mud1_1418=new_t{1};
%%
DiatomOoze_1418=new_t{2};
%%
MudstoneDiamict_1418=new_t{3};
%%
SiltstoneMudstone_1418=new_t{4};
%%
Mud2_1418=new_t{5};
%%
Sand_1418=new_t{6};
%%
BioMud1_1418=new_t{7};
%%
BioMud2_1418=new_t{8};
%%
CalcMud_1418=new_t{9};
%%
DiatomMud1_1418=new_t{10};
%%
DiatomMud2_1418=new_t{11};
%%
MuddyDiamict_1418=new_t{12};
%%
SiltMud_1418=new_t{13};
%%
VolcanicMud_1418=new_t{14};
%%
Mud_1418=[Mud1_1418;Mud2_1418];
%%
Carbonate_1418=[DiatomOoze_1418;BioMud1_1418;BioMud2_1418;CalcMud_1418;DiatomMud1_1418;DiatomMud2_1418];
Carbonate_1418_data = [table2array(Carbonate_1418(:,2)) table2array(Carbonate_1418(:,5:8))];
%%
Siliciclastic_1418=[Mud_1418;SiltMud_1418;Sand_1418;MudstoneDiamict_1418;SiltstoneMudstone_1418;MuddyDiamict_1418;];
Siliciclastic_1418_data = [table2array(Siliciclastic_1418(:,2)) table2array(Siliciclastic_1418(:,5:8))];
%%
Volcanic_1418=[VolcanicMud_1418];
Volcanic_1418_data = [table2array(Volcanic_1418(:,2)) table2array(Volcanic_1418(:,5:8))];
%%
figure(2)
plot(Siliciclastic_1418_data(:,3),Siliciclastic_1418_data(:,1),'b.',Carbonate_1418_data(:,3),Carbonate_1418_data(:,1),'k.',Volcanic_1418_data(:,3),Volcanic_1418_data(:,1),'g.');
axis ij
xlabel ('Porosity(%)');
ylabel ('Depth(m)');
title('Alaska 1418 Porosity Vs Depth');
hold on
Voss
Voss on 31 Dec 2022
You're welcome!
I would start by taking the code I posted in my answer that generates the second plot, and use it as-is with your other data file.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!