2 views (last 30 days)

Hi!

I am trying to use subplot and was looking for tips on how to approach this:

There's a folder full of N .mat files, and each file contains three variables that I want to subplot (R, A, and P). Each .mat file has a unique name beginning with its date.

I am trying to subplot N rows, with 3 columns R, A and P, so that I can see and compare them all together like a gallery.

My intended code goes through each .mat file of N .mat files, loads and plots its R, A and P as columns 1, 2 and 3 of row 1 of N files.

Then it goes to the next .mat file, and loads and plots its R, A and P as columns 1, 2 and 3 of row 2 of N... etc.

..And I'd like all N to be in the same figure. I want to generate a giant figure with, say, 70 rows, and compare R, A and P as three columns alongside each other.

** Edited one day after posting to include my current approach** --

To start with, I am trying to subplot from a test folder containing 3 mat files, and am only plotting R and A variables) (so it's a 3X2 subplot).

I am using a nested for loop and an if.

Here is the code I have so far:

%%

read_folder = '...';

write_folder = '...';

cd(read_folder);

files = dir('*.mat');

N=numel(files);

R_y = [array of numbers]';

A_y = [another array of numbers]';

figure;

fig = gcf;

for i = 1:N

for j = 1:6 %will need to change this as per numel(files).

load(files(i).name, 'R', 'A')

[pathstr,name,ext] = fileparts(files(i).name);

newFilename = fullfile(pathstr,name);

subplot(N,2,j)

if mod(j,2) == 0

subplot(N,2,j);

plot(A_y, A);

else mod(j,2) == 1 ;

subplot(N,2,j);

plot(R_y, R);

end

end

cd(write_folder);

saveas(fig, Image_name, 'png');

cd(read_folder);

end

This makes a 3x2 subplot, but it plots the R and A from just the one mat file in all three rows instead of plotting from each mat file, which is what I'd like for it to do.

Thanks so much for all your tips!! :-)

Pujitha Narra
on 6 Jan 2020

As far as I understand, there must 6 subplots(for the sample code written) in a single plot, each row representing a .mat file.

But the code above is trying to make 6 subplots for each of the .mat file which explains the anomoly you are seeing.

Small changes to the code will help you.

j=1;

for i = 1:N %number of .mat files

subplot(N,3,j)

%....code to plot first variable here

j=j+1;

subplot(N,3,j);

%....code to plot second variable here

j=j+1;

subplot(N,3,j);

%....code to plot third variable here

j=j+1;

end

Opportunities for recent engineering grads.

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

Start Hunting!
## 8 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782588

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782588

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782589

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782589

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782858

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782858

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782859

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782859

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782861

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782861

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782865

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782865

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782882

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_782882

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_783373

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/498729-subplot-the-same-three-variables-from-n-mat-files-in-a-folder#comment_783373

Sign in to comment.