Combines all the measurements into a single matrix
61 次查看(过去 30 天)
显示 更早的评论
"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:
- Imports data for multiple samples separately.
- 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
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
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
2024-11-19,8:52
Hi @Nadia
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!