Combines all the measurements into a single matrix

조회 수: 71 (최근 30일)
Nadia
Nadia 2024년 11월 19일 8:12
댓글: Mathieu NOE 2024년 11월 20일 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
Mathieu NOE
Mathieu NOE 2024년 11월 20일 14:06
편집: Mathieu NOE 2024년 11월 20일 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 2024년 11월 20일 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

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

답변 (1개)

Umeshraja
Umeshraja 2024년 11월 19일 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
Nadia
Nadia 2024년 11월 19일 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

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by