how to plot contour ?

How to do the below mentioned process in the loop and save contour. Please share your knowledge. Attached data for your reference.
I tried to create a loop but got the Error: Invalid array indexing. T = readtable('HB2B_2530.csv','PreserveVariableNames',true); Vx = table2array(T(:,:));%X TTT = Vx(:,10:13); A=unique(TTT,'rows'); Acell=splitapply(@(x){x}, A,findgroups(A(:,2))); Acell{:}; y1 = [Acell{1,:}];%first plane values y2 = [Acell{2,:}];%second plane values y3 = [Acell{3,:}];%third plane values y4 = [Acell{4,:}];%fourth plane values for i= 1:4 rg(i) = linspace(min(y(i)(:,1)), max(y(i)(:,1)), 100); cg(i) = linspace(min(y(i)(:,3)), max(y(i)(:,3)), 100); [Rg(i), Cg(i)] = meshgrid(rg(i), cg(i)); Zg(i) = griddata(y(i)(:,1), y(i)(:,3), y(i)(:,4), Rg(i), Cg(i)); contourf(Rg(i),Cg(i),Zg(i)); end

2 Comments

I am not sure what you mean by "save contour in auto" ?
Acell{:};
What is the purpose of that line?
Is there a reason you are using readtable() followed by table2array() instead of using readmatrix() ?
MS
MS on 16 Dec 2022
No reason. I am unable to run in the loop

Sign in to comment.

 Accepted Answer

T = readtable('HB2B_2530.csv','PreserveVariableNames',true);
Vx = table2array(T(:,:));%X
TTT = Vx(:,10:13);
A = unique(TTT,'rows');
Acell = splitapply(@(x){x}, A,findgroups(A(:,2)));
Acell{:};
y1 = [Acell{1,:}];%first plane values
y2 = [Acell{2,:}];%second plane values
y3 = [Acell{3,:}];%third plane values
y4 = [Acell{4,:}];%fourth plane values
y = {y1, y2, y3, y4};
N = length(y);
rg = cell(N,1); cg = cell(N,1);
Rg = cell(N,1); Cg = cell(N,1);
Zg = cell(N,1);
for i= 1:N
rg{i} = linspace(min(y{i}(:,1)), max(y{i}(:,1)), 100);
cg{i} = linspace(min(y{i}(:,3)), max(y{i}(:,3)), 100);
[Rg{i}, Cg{i}] = meshgrid(rg{i}, cg{i});
Zg{i} = griddata(y{i}(:,1), y{i}(:,3), y{i}(:,4), Rg{i}, Cg{i});
subplot(N,1,i);
contourf(Rg{i}, Cg{i}, Zg{i});
end
Acell will be a cell array in which each matrix is a composed of rows extracted from the array A that has four columns. It will have as many entries as there are distinct groups in the data. The code assumes that there are at least 4 such groups. Acell will have as many columns as findgrous(A(:,2)) has, which would be 1, so Acell will be som
So Acell{1,:} would be the same as Acell{1,1} since Acell will be something x 1. Since there is only one cell being accessed, the result of [Acell{1,1}] would be the same as if you had used Acell{1} directly .
Now suppose that somehow you had Acell being an N x 2 cell array such that Acell{1,:} would be different than Acell{1} . In such a case [Acell{1,:}] would be [Acell{1,1}, Acell{1,2}] which would be expecting to be able to horizontally concatenate those two arrays. But do we know if those (hypothetical) arrays are even the same height to be able to [] them together ? If these hypothetical arrays are, hypothetically, the same height then the result of the [] would have more than 4 columns, but your code processing y ignores anything past the 4th column... so why bother to [] the (hypothetical) rows together?

7 Comments

MS
MS on 17 Dec 2022
Edited: MS on 17 Dec 2022
@Walter Roberson is it possible to save resutls contours automatically? Please let me know.
h = figure
% subplot(N,1,i);
contourf(Rg{i}, Cg{i}, Zg{i});
saveas(h,sprintf('FIG%d.png',i));
Yes something like that could work. We recommend exportgraphics() though
MS
MS on 17 Dec 2022
Edited: MS on 17 Dec 2022
Many thanks for everthing. could you please let me know the way to use export graphics for the current code?
Change
saveas(h,sprintf('FIG%d.png',i));
to
exportgraphics(h,sprintf('FIG%d.png',i)));
You might find, by the way, that you want to save the axes rather than the figure. For example,
T = readtable('HB2B_2530.csv','PreserveVariableNames',true);
Vx = table2array(T(:,:));%X
TTT = Vx(:,10:13);
A = unique(TTT,'rows');
Acell = splitapply(@(x){x}, A,findgroups(A(:,2)));
Acell{:};
y1 = [Acell{1,:}];%first plane values
y2 = [Acell{2,:}];%second plane values
y3 = [Acell{3,:}];%third plane values
y4 = [Acell{4,:}];%fourth plane values
y = {y1, y2, y3, y4};
N = length(y);
rg = cell(N,1); cg = cell(N,1);
Rg = cell(N,1); Cg = cell(N,1);
Zg = cell(N,1);
for i= 1:N
rg{i} = linspace(min(y{i}(:,1)), max(y{i}(:,1)), 100);
cg{i} = linspace(min(y{i}(:,3)), max(y{i}(:,3)), 100);
[Rg{i}, Cg{i}] = meshgrid(rg{i}, cg{i});
Zg{i} = griddata(y{i}(:,1), y{i}(:,3), y{i}(:,4), Rg{i}, Cg{i});
ax = subplot(N,1,i);
contourf(ax, Rg{i}, Cg{i}, Zg{i});
exportgraphics(ax,sprintf('FIG%d.png',i)));
end
this displays the contour in context but also saves each one individually.
MS
MS on 19 Dec 2022
Edited: MS on 19 Dec 2022
Thanks @Walter Roberson. Part of the figure is not saving while using the code. I mean edges are missing in the saved images. Please let me know the reason and any changes required.
clear all
close all
clc
set(0,'DefaultaxesFontSize',20);
set(0,'DefaulttextFontsize',20);
set(0,'DefaultaxesFontName','Times-Roman');
set(0,'DefaulttextFontName','Times-Roman');
set(0,'defaulttextinterpreter','latex')
set(0, 'DefaultAxesLineWidth', 4)
set(0,'DefaultAxesFontWeight','bold')
set(gcf, 'DefaultLineLineWidth', 4)
set(gca, 'DefaultLineLineWidth', 4)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T = readtable('HB2B_2530.csv','PreserveVariableNames',true);%reading the data
Vx = table2array(T(:,:));%converting to arrays
TTT = Vx(:,10:13);%Pick your columns based on the data.EG., dsapce columns number = 13
A = unique(TTT,'rows');
Acell = splitapply(@(x){x}, A,findgroups(A(:,1)));%split group based on column 2=vy
Acell{:};
y1 = [Acell{1,:}];%first plane values
y2 = [Acell{2,:}];%second plane values
y3 = [Acell{3,:}];%third plane values
y4 = [Acell{4,:}];%fourth plane values
y5 = [Acell{5,:}];%fourth plane values
y6 = [Acell{5,:}];%fourth plane values
y7 = [Acell{7,:}];%fourth plane values
y = {y1, y2, y3, y4,y5,y6,y7};
N = length(y);
rg = cell(N,1); cg = cell(N,1);
Rg = cell(N,1); Cg = cell(N,1);
Zg = cell(N,1);
for i= 1:N
rg{i} = linspace(min(y{i}(:,2)), max(y{i}(:,2)), 100);
cg{i} = linspace(min(y{i}(:,3)), max(y{i}(:,3)), 100);
[Rg{i}, Cg{i}] = meshgrid(rg{i}, cg{i});
Zg{i} = griddata(y{i}(:,2), y{i}(:,3), y{i}(:,4), Rg{i}, Cg{i});%important change acoriding to data
h = figure
% subplot(N,1,i);
[CC,HH]= contourf(Rg{i}, Cg{i}, Zg{i});%plot contour
set(HH,'LineColor','none')
colormap('parula');
BB = colorbar
BB.Label.String = 'd space';%change
BB.Label.Rotation = 90;
xlabel('YY')%change
ylabel('ZZ')%change
exportgraphics(h,sprintf('FIG%d.png',i));%change the FIG name to whatever the name you need
end
It is not obvious to me that edges are missing, since you did set the line color to none
MS
MS on 19 Dec 2022
Edited: MS on 19 Dec 2022
Thanks @Walter Roberson. i am discussing about the whole image is not saving automatically. I am attaching a full image wiht color label naming. Color label name is mssing while saving it automatically.

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Tags

Asked:

MS
on 16 Dec 2022

Edited:

MS
on 19 Dec 2022

Community Treasure Hunt

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

Start Hunting!