how to convert from table to 3D array

2 views (last 30 days)
Eddy van der Goot
Eddy van der Goot on 17 Jul 2018
Answered: Peter Perkins on 3 Aug 2018
Hello, I have a 1989 x 8 table with column names [ wavelength, Dark, Reference, Amplitude_Capture1, Amplitude_Capture2, Amplitude_Capture3, Amplitude_Capture4, Amplitude_Capture5] I would like to create a 3D array from this table with every first column of the array to be the information of the Wavelength and every second column the amplitude_capture. This would result in:
  • Wavelength and Amplitude_Capture1 in My_Array(:,:,1)
  • Wavelength and Amplitude_Capture2 in My_Array(:,:,2) etc.
I solved this know with the following code:
% code
for i = 1:1:FolderIndex-1
%cd (MyFolderInfo(i).folder)
name=MyFolderInfo(i).name
Names(1,i)=name
MyTable=importfile(name,3,inf)
TableToArrayStr(:,1:8)=table2array(MyTable(:,1:8));
while ColIndex < 9
AmplitudeArray(:,4,(ArrayDimension+Index))=TableToArrayStr(:,ColIndex)
AmplitudeArray(:,1:3,(ArrayDimension+Index))=TableToArrayStr(:,1:3)
Index = Index+1
ColIndex=ColIndex+1
end
ColIndex=4
end
this is a time consuming effort I wonder if it can be faster / optimized.
I did allocate the Arrays before use.
Thanks.

Answers (1)

Peter Perkins
Peter Perkins on 3 Aug 2018
Based on your description, it sounds like you want to horzcat the amplitudes, reshape them into 3D, and prepend (copies of?) the wavelengths. Something like
amps = mytable{:,{'Amplitude_Capture1 ...'Amplitude_Capture5'}};
wavelens = mytable.Wavelength;
ampsData = [repmat(wavelens,1,1,5) reshape(amps,[],1,5)]

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!