Extract text from .txt file, paste in excel

1 回表示 (過去 30 日間)
Eleanor Brian
Eleanor Brian 2023 年 3 月 3 日
回答済み: Voss 2023 年 3 月 5 日
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

回答 (1 件)

Voss
Voss 2023 年 3 月 5 日
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);

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by