Combines all the measurements into a single matrix

61 visualizzazioni (ultimi 30 giorni)
Nadia
Nadia il 19 Nov 2024 alle 8:12
Commentato: Mathieu NOE il 20 Nov 2024 alle 14:19
"I have limited scripting experience but understand that loops are powerful tools for automating repetitive tasks, and I want to use them to save time.
Currently, I need help writing a script that:
  1. Imports data for multiple samples separately.
  2. Combines all the measurements into a single matrix. For example:
  • The first column should represent the wavelengths (constant for all samples).
  • The remaining columns should represent the intensities for each sample.
The dataset size is 5826 rows by 156 columns. Could someone guide me on how to approach this using loops?"
  8 Commenti
Mathieu NOE
Mathieu NOE il 20 Nov 2024 alle 14:06
Modificato: Mathieu NOE il 20 Nov 2024 alle 14:17
hello again
a simple script below that will stack your data (assuming as you posted , the first column with wavelength is valid for all files)
to test my code I simply duplicated your Data.txt file in multiple copies of it , and so we are sure the data are processed in the way we want
not sure if that matters to you , but in case you need to proceed the files in some sorted way I recommend you use this Fex submission (excellent work !) , because dir only is not reliable in all circumstances
if you need to add some processing of your data (I see you want to compute some means here) - we can esily adapt the code accordingly
%% define path
fileDir = pwd; % or your specific path / folder
S = dir(fullfile(fileDir,'Data*.txt')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order (what matlab dir does not well) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
Nfiles = length(S); % Number of files
combined_data= [];
%% Loop inside folder
for k = 1:length(S) % read data
fileName = S(k).name;
data = readmatrix(fullfile(fileDir, fileName)); % or use a structure (S(k).data ) to store the full data structure
% For the first file, store wavelengths in the first column
if k == 1
% store first time : Wavelengths + Intensities
combined_data = data;
else
% next files
combined_data= [combined_data data(:, 2:end)]; % then add Intensities only (skipp first column Wavelengths) for the next files
end
end
Mathieu NOE
Mathieu NOE il 20 Nov 2024 alle 14:19
here some further code extension if you want to add a "mean intensity" column (appended at the end ) for each file -
%% define path
fileDir = pwd; % or your specific path / folder
S = dir(fullfile(fileDir,'Data*.txt')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order (what matlab dir does not well) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
Nfiles = length(S); % Number of files
combined_data= [];
%% Loop inside folder
for k = 1:length(S) % read data
fileName = S(k).name;
data = readmatrix(fullfile(fileDir, fileName)); % or use a structure (S(k).data ) to store the full data structure
% For the first file, store wavelengths in the first column
if k == 1
% store first time : Wavelengths + Intensities and add "mean" data to the right
combined_data = [data mean(data(:, 2:end),2)];
else
% next files
combined_data= [combined_data data(:, 2:end) mean(data(:, 2:end),2)]; % then add Intensities + mean Intensity only (skipp first column Wavelengths) for the next files
end
end

Accedi per commentare.

Risposte (1)

Umeshraja
Umeshraja il 19 Nov 2024 alle 8:52
I understand you're looking to combine your measurements into a single matrix. Assuming your input files are text files with two columns (wavelength and intensity), here's a script to help you achieve this:
It combines by reading each sample file, Store wavelengths (only from the first file since they're constant), Store intensities from each file in subsequent columns
num_samples = 156; % Number of files
num_rows = 5826;
% Initialize with zeros
% First column will be wavelengths, other columns will be intensities
combined_data = zeros(num_rows, num_samples);
% Loop through each sample file
for i = 1:num_samples
filename = sprintf('sample_%d.txt', i);
% Assuming space-delimited text files with two columns:
data = readtable(filename, "Delimiter", " ");
% For the first file, store wavelengths in the first column
if i == 1
combined_data(:, 1) = data{:, 1}; % Wavelengths
end
combined_data(:, i + 1) = data{:, 2}; % Intensities
end
% Save the combined data
save('combined_measurements.mat', 'combined_data');
For more details on using the 'readtable' function, you can refer to the following MATLAB documentation
Hope it helps!
  1 Commento
Nadia
Nadia il 19 Nov 2024 alle 9:25
I am trying to work with it, but yet not sucessful. I also want to combine the mean of matricx. As 1 or 2 mean spectra per sample.
For example put together one matrix each for high and low intensties

Accedi per commentare.

Categorie

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

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by