How to plot all the graphs from different folder and save them in a different folder by name of folder

10 views (last 30 days)
Hi,
I have 44 folders in the path below and each folder have 1 excel file. Is there a way I can change the code below in a way that it will go into folder by folder and plot the graph using the data in that folder and save the graph in .emf format by the folder name in the path folder. Path is shown below as well as the code I am using. It is basically going to take me alot of time by doing one by one. I am also attaching the picture which will show folder names
Path where all the folders are: F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\6-VectorSatistics\PlotOverLine
Current Code: (Basically going one by one into folder and changing the path each time to save the graph)
files = ['F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\6-VectorSatistics\PlotOverLine\Run 13-33-42.Profile Plot.71gpvfq7\Export.71gpvfq7.000000.csv'];
a = readmatrix(files);
length=a(:,2);
U_x=a(:,11);
U_y=a(:,12);
U_m=a(:,25);
plot (length,[U_x,U_y,U_m],'-');
%Chart Representation
title('Velocities Across VFC (Below Outlet)');
xlabel('Distance(mm)');
ylabel('Velocities (m/s)');
legend('Velocities in x-direction','Velocities in y-direction','Resultant-Velocities')

Accepted Answer

Stephen23
Stephen23 on 16 Jul 2022
Edited: Stephen23 on 16 Jul 2022
"It is basically going to take me alot of time by doing one by one."
Don't! Computers are really only good at one thing: very simple tasks repeatedly in loops. When you copy-and-paste code like that you are just doing the computer's job for it.
Probably the simplest approach is to let DIR get the names of those subfolders and then you can simply loop over them:
P = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\6-VectorSatistics\PlotOverLine';
S = dir(fullfile(P,'Run*Profile*','Export*.csv'));
% ^^^^^^^^^^^^^ % match subfolder names
% ^^^^^^^^^^^^ % match datafile names
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
M = readmatrix(F);
% do whatever with the matrix M
end
Note:
  • modify the DIR match string to suit your folder/filenames. Read the DIR documentation and experiment: always check the size of S before and after modifying the match string.
  • if you do not want to simply discard the results (plots, values, whatever) of all loop iterations then you will also need to store or save those values or plots (e..g. by allocating to an output array, by exporting to file, by tiling plots on a figure, etc.).
  4 Comments
muhammad choudhry
muhammad choudhry on 17 Jul 2022
you been very helpful, just last question tho I am playing around with the presentation but the presentation is also coming on last graph just no matter where I am putting inside the loop or outside the loop.
%Chart Representation
title('Velocities Across VFC (Below Outlet)');
xlabel('Distance(mm)');
ylabel('Velocities (m/s)');
legend('Velocities in x-direction','Velocities in y-direction','Resultant-Velocities')

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB 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!