Clear Filters
Clear Filters

How to use a for loop using contourfmap method ?

3 views (last 30 days)
S Ch
S Ch on 25 Jun 2021
Answered: Animesh on 8 Apr 2024
Hello,
I used the contourfcmap and I directly chose the number of levels I want, unfortunately I'm struggling to do a loop of 100 images using this method ! anyone can help me please?
To explain the problem, I have 100 matrices in a folder (each matrix is an image that I want to contourfmap) and after that save them in a folder as images with the colors I chose in contourfmap. Is this possible ?
This is my code:
for i = 1:N
%names is the char name of all matrices
if i < 10
names(i,:) = strcat("X_t0000",string(i),".mat");
else
names(i,:) = strcat("X_t000",string(i),".mat");
end
% A = eval(names);%(i,:)) ;
%After that I didn't know how to use names so I change to this follows:
Data= load(F(i).name);
z=Data.data; %iside each matrix there is matrix named 'data'
h1 = contourfcmap(v,x,z,[0.1 0.33],jet(1),[.1 .1 .1], [.9 .9 .9], 'eastoutside')
d = h1.c
l=h1.h
export_fig(fullfile(Folder, sprintf('%05d.png', i)))
%%% Here I had only one image appeared I think the last one.
% imwrite(d,fullfile(Folder, sprintf('%05d.png', i)))
% A = cat(3,names); %Here I wanted to concatenate the 100 matrices into
% one like 2048x2048x100 but I didn't succed
end
If it isn't clear please tell me to explain more. Thank you in advance.
S.C

Answers (1)

Animesh
Animesh on 8 Apr 2024
Hi @S Ch,
I understand that you are trying to loop through 100 matrices, apply "contourfcmap" to each, and then save the resulting images. Here is a modified version of your code that should work:
% Assuming 'F' is a struct array with all the file names
for i = 1:length(F)
% Load the data
Data = load(F(i).name);
z = Data.data; % Inside each matrix there is matrix named 'data'
% Apply contourfcmap and get the handle
h1 = contourfcmap(v, x, z, [0.1 0.33], jet(1), [.1 .1 .1], [.9 .9 .9], 'eastoutside');
% Save the figure
export_fig(fullfile(Folder, sprintf('%05d.png', i)));
% Close the figure to prevent MATLAB from superimposing all figures
close(gcf);
end
This code will load each matrix, apply "contourfcmap", and then save the figure as a PNG file in the specified folder. The "close(gcf)"; line is important because it closes the current figure after saving it, which prevents MATLAB from superimposing all the figures on top of each other.
As for concatenating the matrices, you can initialize an empty 3D matrix before the loop and then concatenate each 2D matrix along the third dimension inside the loop. Here is how you can do it:
% Initialize an empty 3D matrix
A = [];
for i = 1:length(F)
% Load the data
Data = load(F(i).name);
z = Data.data; % Inside each matrix there is matrix named 'data'
% Concatenate 'z' along the third dimension
A = cat(3, A, z);
% Rest of your code...
end
This will result in A being a 3D matrix of size 2048x2048xN (where N is the number of matrices).

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!