read data from file and convert to a 3d array.

조회 수: 4 (최근 30일)
vampcj
vampcj 2019년 7월 5일
편집: Rik 2019년 7월 8일
So, I have a dat file storing a 3d data (131*385*97), the data is store in form below:
a(1,1,1),a(1,1,2)...........a(1,1,97)
a(1,2,1),a(1,2,2)...........a(1,2,97)
. . . . . . . . . . .
a(1,385,1),a(1,385,2)...........a(1,385,97)
a(2,1,1),a(2,1,2)...........a(2,1,97)
. . . . . . . . . . .
a(131,385,1),a(132,385,2)...........a(131,385,97)
My question is how I read this form of data and store it as a 3d array in matlab?

답변 (2개)

Abhishek Kumar
Abhishek Kumar 2019년 7월 8일
편집: Abhishek Kumar 2019년 7월 8일
I am attaching the code below this works for data (3*3*3), make changes accordingly to suit your purpose.
Also, If you wish to read data from a dat file, you will have to use the function importdata(..)
for more information read the documentation or try
>> help importdata
a = reshape(1:27, 3, 9) .'; % here i have data which is 9*3
i=1;
m=1;
n=1;
o=1;
for j =1:9
o=1;
for k=1:3
A(m, n, o)=a(j,k);
o=o+1;
if mod(j, 3)==0 && i~=3 && k==3
o=0;
n=0;
m=m+1;
end
end
n=n+1;
end

Rik
Rik 2019년 7월 8일
편집: Rik 2019년 7월 8일
To suggest the best way to read the file, you will have to share a small sample. Otherwise functions like importdata or the more low-level textscan will probably do the job.
For the parsing of the data once you have read the file, you can use the code below. As you can see with the check at the end, the output is as expected. There is no way to automatically determine how many rows and columns there are, so you will have to provide at least one of the two. The number of pages is simply the second dimension of your input, and once you have rows or cols you can divide the length of the second input by the one to obtain the other.
cols=3;rows=4;pages=5;
data=reshape(1:cols*rows*pages,cols*rows,pages);%generate random data
%each cell is the third dimension
b=mat2cell(data,ones(1,size(data,1)),size(data,2));
b2=reshape(b,rows,cols)';
flip_to_third_dim=@(x) permute(x(:),[3 2 1]);
out=cellfun(flip_to_third_dim,b2,'UniformOutput',false);
out=cell2mat(out);
%check:
isequal(out(1,1,:),flip_to_third_dim(data(1,:)))

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by