Append Column from Loaded Data to Matrix in a Loop

Hi, I am trying to load data into a matrix. Every iteration should result in a new column. My code currently appends everything in 1 column as a new row.
Open to any simplifications or suggestsions! I am new to matlab and have been using this load code as my go-to.
TYIA
spectra_dir = uigetdir; %gets directory
my_spectras = dir(fullfile(spectra_dir,'RSN*.txt')); %gets all txt files in struct
spectras_load = []; %creates matrix from data in each vector
for k = 1:length(my_spectras)
baseFileName = my_spectras(k).name;
fullFileName = fullfile(spectra_dir, baseFileName);
spectra_file = load(fullFileName);
psa_spectra = spectra_file(:, 2);
spectras_load = [spectras_load; {psa_spectra}];
end
spectras = cell2mat(spectras_load);

답변 (1개)

the cyclist
the cyclist 2022년 3월 7일
Changing this
spectras_load = [spectras_load; {psa_spectra}];
to this
spectras_load = [spectras_load, {psa_spectra}]; % Notice use of comma rather than semicolon
Beware aware that building arrays by adding columns is very inefficient for memory management, and can become very slow. You could use preallocation to speed this code.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2022년 3월 7일

답변:

2022년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by