Read Numbers from .dat file in the format of a 3d matrix

4 views (last 30 days)
Hi, I have a .dat file as below:
1, 2, 4, 2, 4
3, 4, 6, 1, 5
...
They are 40 numbers, 8 lines and 5 columns.
Now,
I want to read it by matlab as a 3d matrix: 4*2*5
If you suggest textscan please give me some hints how to use it. Thanks alot!

Accepted Answer

Jan
Jan on 7 Oct 2013
There are many different versions to reshape the data to 4x2x5. Perhaps you want:
fid = fopen(FileName, 'r');
if fid == -1, error('Cannot open file for reading.'); end
data = fscanf(fid, '%g,%g,%g,%g,%g', [5, Inf]);
data = reshape(transpose(data), [4,2,5])
fclose(fid);

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!