MATLAB Answers

how to load multiple text files in nested folder?

1 view (last 30 days)
Hello!
I'm new to matlab -- I am trying to load multiple txt files in a nested folder:
e.g. HCA1/ts/cortex.txt
HCA2/ts/cortex.txt
HCA3/ts/cortex.txt
HCA4/ts/cortex.txt
I want to plot these on the same graph, and I have done this successfully with one subject, but I haven't been able to figure it out with many subjects. If possible, I would love to just be able to use a * or a variable to define all of the subjects, so that i don't have to individually write them in..
Current Code:
%% loading ventricle and cortex ts (make sure you are in the folder of these ts!!!!) and detrending
l = load('lcortex_run01regPA.txt')
v = load('ventricle_run01regPA.txt')
s = load('spinal_run01regPA.txt')
%naming of subject
name = 'HCA1'
%v_ts = run02_ventricle_segmentation_stcfsl;
c_ts = l
v_ts = v
s_ts = s
%% detrend ventricle and cortex
d_v_ts = detrend(v_ts) %detrended time series of ventricle
d_c_ts = detrend(c_ts) %detrended time series of cortex
d_s_ts = detrend(s_ts) %detrended time series of spinal cord
%% defining timeseries x axis - plotting ventricle, cortex, spinal cord -raw fmri signal
whole_ts = 1:478 %your ventricle timeseries
ttt = whole_ts*.800 %the TR found from HCP mri_info
figure;
ax1 = subplot(3,1,1)
hold on
plot (ttt, d_v_ts)
plot (ttt, d_c_ts)
plot (ttt, d_s_ts)
hold off
legend('ventricle','cortex','spinal cord')
sgtitle(name)
xlabel('Time')
ylabel('Raw fMRI signal')
%ylim([-170 170])
Thank you!!

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 28 Dec 2019
Probably the sprintf method does what you need:
For example, something like this:
D = 'absolute or relative path to the directory where the subdirectories are';
for k = 1:4
name = sprintf('HCA%d',k); % HCA1, HCA2, etc.
l = load(fullfile(D,name,'lcortex.txt'));
v = load(fullfile(D,name,'ventricle.txt'));
s = load(fullfile(D,name,'spinal.txt'));
... your code
... you will probably want a SAVEAS or PRINT here.
end

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.