Clear Filters
Clear Filters

Running a loop of inputting tables into MatLab

4 views (last 30 days)
Hi, I have this code:
[data]=loadScopeData('/Volumes/usb/table_5.csv',[1,inf]);
max_col2_5=max(data.pvdf_v)
max_col3_5=max(data.cell_v)
and I have tables for each trial 5-40 and I was wondering if there was a way that I could run this in a loop so import each table (table_5, table_6, table_7...), and to find the max of each of the second two columns labeled (max_col2_5, max_col2_6, max_col2_7...) matching the number of the table, so that I can later make a table out of these values. I have bolded the numbers that would need to increase by one each time, the rest of teh script would stay the same. I am thinking that a while loop could work for this, but I'm not sure how I can integrate that into the file name when loading the data. Turning this into a loop would be a lot faster and easier to run that having this script for each trail, especially as I start doing more. Thanks.

Accepted Answer

Voss
Voss on 20 Jun 2024
Edited: Voss on 20 Jun 2024
First generate the file names. Since you know it's trials 5-40, you can do this:
filenames = "/Volumes/usb/table_" + (5:40) + ".csv";
(If you didn't know in advance which files you'd need, you could use dir and fullfile to get the file names and then likely natsortfiles to put them in the right order.)
Then, in a loop, read each file and collect the relevant values into a matrix:
N = numel(filenames);
max_col = zeros(N,2);
for ii = 1:N
data = loadScopeData(filenames(ii),[1,inf]);
max_col(ii,1) = max(data.pvdf_v);
max_col(ii,2) = max(data.cell_v);
end
If later you want to make a table out of those values then:
T = array2table(max_col);

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!