Extract text from .txt file, paste in excel

1 visualizzazione (ultimi 30 giorni)
Eleanor Brian
Eleanor Brian il 3 Mar 2023
Risposto: Voss il 5 Mar 2023
Hi there,
I have >60 people in my study, all with a code name MNP_001 - MNP_060. An example of the folder layout: /Documents/MRI_data/FM/MNP_005/MNP_005/IDP_files
In IDP files there is a text file ("IDP.txt") with 800 numbers seperated by a space. I would like a script to copy these numbers from the text file and paste into excel where each space means paste on a new row. I would then like a batch script to copy for the other 59 participants and include into the same excel. If there was an error in the script, some patients may have NaN instead of some numbers, I'd like to still paste NaN where a number should be and to ensure the script doesn't break with this.
So the end table would be as follows: 60 columns, each labelled with the MNP number for that participant. 800 rows, for each of the pasted numbers from the text file seperated by a space.
Please can someone help me to make this script?
Thanks!! Eleanor

Risposte (1)

Voss
Voss il 5 Mar 2023
Maybe something like this:
folder = '/Documents/MRI_data/FM/';
N_files = 60;
N_rows = 800;
str = compose('MNP_%03d',1:N_files);
file_names = fullfile(folder,str,str,'IDP_files','IDP.txt');
output_file_name = fullfile(folder,'IDP_all.csv');
data = zeros(N_rows,N_files);
for ii = 1:N_files
new_column = readmatrix(file_names{ii});
data(:,ii) = new_column;
end
t = array2table(data,'VariableNames',str);
writetable(t,output_file_name);

Categorie

Scopri di più su Data Import from MATLAB 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