How to create 3D arrray from text file?

New to MATLAB. I want to create a graph from the attached .txt file. The first 11 rows are just descriptive information. The file then lists each XZ plane as a 2D table, where the outer row and column are the axes. Is there a way that I can read the "XZ plane" value and each plane into a 3D array?
Thank you.

1 Comment

Yes. But you'll have to write specific code to do so -- @doc:textscan will be the tool. To skip the first header lines use the 'HeaderLines' named parameter option.
The format string will be something like
fmt=['%fnm' repmat('%f',1,49)];
I didn't check definitively about the delimiter; looks to be blank/space delimited, not tabs.
You'll have to read each group/plane separately and loop through for the 50 planes. Since the dimensions are given a priori, you can preallocate the output array and store into it.
NB: textscan will return a cell array containing the doubles; use cell2mat on it before putting into the 3D array.

Sign in to comment.

Answers (1)

OK, had a little time --
XZ=zeros(50,50,50); % preallocate the output array
fmt=['%fnm' repmat('%f',1,50)]; % format string for each data record
fid=fopen('20 keV energy by position graph1.txt','r'); % open file for input
% get first plane
data=cell2mat(textscan(fid,fmt,'HeaderLines',13,'delimiter','\t','CollectOutput',1));
XZ(:,:,1)=data(:,2:end); % save plane, strip off coordinates
Z=data(:,1); % Z coordinates
% now get rest
for i=2:50
data=cell2mat(textscan(fid,fmt,'HeaderLines',2,'delimiter','\t','CollectOutput',1));
XZ(:,:,i)=data(:,2:end); % save plane, strip off coordinates
end
fid=fclose(fid);
The above didn't read the X,Y coordinates; can either scan the header records for the range/divisions and compute or read record 13 for X and the first of the two header records by group for Y. For this file, X and Y are same so only need one; I presume this may not be the case universally so would need to extract from file for complete generality.

Categories

Tags

Asked:

on 23 Jul 2022

Answered:

dpb
on 23 Jul 2022

Community Treasure Hunt

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

Start Hunting!