I need to read a specific column from 30 files in a folder (read the same column from each of them) and plot each column as a line. How can I go about it?

11 visualizzazioni (ultimi 30 giorni)
I have a bunch of files with columns of data, all saved to a folder that has only these files in it. I need to load the data from each of these files and then plot one of the columns of each file (the same column in each file), each column being a different line in the same plot. The name of the files, while similar, is different for each (they are all 'PowerMat_(array of parameters which is different for each file).mat).
I have created a code that will read one file, the column I need and plot it against an x axis that I chose. This code (linked below) will read the file from the workspace as I uploaded beforehand. But this is not what I want for the code I need to produce. I want it to read data from the directory itself as it would be too time consuming to upload each file onto the workspace as eventually I will be working with hundreds of files. Therefore I need to create a for loop to read and plot all of these files at once, and I am struggling to find the correct way of doing this.
Thank you.
x = 15 : 1 : 50 ; %Array of z values on zmin_15 (from 15 to 50 in steps of 1)
R = 30 %Number of runs of the simulation
n = 39 ; %Nº of column to read of the PowerMat (each column is wavevector value)
y = PowerMat_1 (: , n) ; %Selecting column n on file
figure ; %Open separate figure window
plot (x , y) ; %Plot for exampleSim
set(gca, 'YScale', 'log') %Sets y axis to be logarithmic scale. Comment for linear
title('Power Spectrum vs Redshift at k=') ;
xlabel('Redshift (Z)') ;
ylabel('Power Spectrum') ;

Risposta accettata

Mathieu NOE
Mathieu NOE il 6 Lug 2022
hello Agustina
see code below :
% use the structure returned by DIR:
P = cd; % working directory
S = dir(fullfile(P, 'PowerMat_*.mat'));
S = natsortfiles(S); % sort folders / file names into natural order
% (download FEX : https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
% init main code
x = 15 : 1 : 50 ; %Array of z values on zmin_15 (from 15 to 50 in steps of 1)
R = 30 ; %Number of runs of the simulation
n = 39 ; %Nº of column to read of the PowerMat (each column is wavevector value)
figure(1) ; hold on
for k = 1:numel(S)
fn = S(k).name; % filename : used for legend
fn = strrep(fn,'_',' ');% replace _ by space for better legend rendering
leg{k} = fn; % store in cell array
% load data
F = fullfile(S(k).folder, S(k).name);
data = load(F); % is structure
names = fieldnames(data); % get fieldnames
y = data.(names{1}); % your data (assumed in first fieldname)
y = y(:,n) ; %Selecting column n on data
plot(x,y)
end
legend(leg); % put legend
hold off

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by