Need guidance to loop directory, import .csv, get corresponding data from 2nd row and filter first, extract this corresponding data.
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have been working on CFD for my workplace and there is a lot of manual work that could be solved by a MATLAB program. However, I am not familiar with the syntax and logics of coding and MATLAB and I was wondering if you could guide me in the right direction with this.
My task right now is:
- I probe and extract data from the CFD, it gets stored as a . csv file
- I have to do this for every component of a building so I have around 30-40 .csv files
- The .csv files data then has to be copied to corresponding sheets of a master excel file (with pre existing formulas that are based on cells that calculate values) where the formulas would be applied.
What I want to do:
Write a program that :
- loads all .csv files into Matlab as tables or arrays in a loop
- Extracts some values from the .csv, for instance
-Apply formula after extracting value and store the answers
FOR INSTANCE:
From this table:
Case1
Var 1 Var 2 Var3
Area | 262.452 | ft^2
Avg. Temperature | 113.302 | Fahrenheit
Case 2
Area | 262.452 | ft^2
Avg. Temperature | 1234.302 | Fahrenheit
Case 3
Area | 262.452 | ft^2
Avg. Temperature | 11233.302 | Fahrenheit
<COUPLE OF ADDITIONAL ROWS>
Total area | 828.093 | ft^2
I would like to extract all average temp 113.302,1234.302,11233.302 and the total area 828.093 from all the tables > apply a formula to these values > and then store in an excel file.
They are all in the same position in the .csv files
Where I am getting stuck:
-Loop around files in a directory
- Cannot create an array and can only create table
-Do not know how to get corresponding table values for "Avg Temperature" and "Total area
- extracting location from table to apply a formula
0 commentaires
Réponses (1)
Mathieu NOE
le 29 Juin 2023
hi
below some simple code to loop in a folder - we could also loop over multiple folders if we need to
there will be still a little bit of work to do then the data processing, but for that it would definitively help to have some csv files from you
% A simple, efficient solution is to use the structure returned by DIR:
P = pwd; % get working directory (or specify it)
S = dir(fullfile(P, '*.csv'));
% S = natsortfiles(S); % sort folders / file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:numel(S)
F = fullfile(S(k).folder, S(k).name);
S(k).data = csvimport(F); % or READTABLE or whatever.
% here do your post processing
end
% FYI, the structure S contains all of your file data and the corresponding filenames
% For example, the 2nd filename and its data:
% S(2).name
% S(2).data
9 commentaires
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

