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

조회 수: 28 (최근 30일)
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')

채택된 답변

Stephen23
Stephen23 2022년 7월 16일
편집: Stephen23 2022년 7월 16일
"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
muhammad choudhry
muhammad choudhry 2022년 7월 17일
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')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by