- 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.).
How to plot all the graphs from different folder and save them in a different folder by name of folder
5 views (last 30 days)
muhammad choudhry on 15 Jul 2022
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);
title('Velocities Across VFC (Below Outlet)');
legend('Velocities in x-direction','Velocities in y-direction','Resultant-Velocities')
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