MATLAB: How to read a txt file with known structure

char to matrixhowhow to read text filereadfile

Hello, How to read a file that has given structure?
boundary -5.0 -5.0 5.0 15.0
block -3.0 1.0 3.0 1.25
block -3.0 1.0 -2.75 10.0
block 2.75 1.0 3.0 10.0
I want array or matrix of first four digits e.g
boundary=[-5.0 -5.0 5.0 15.0],
and in the block should look like this
block=[ -3.0 1.0 3.0 1.25;-3.0 1.0 -2.75 10.0]
Number of rows is not constant but number of columns are constant so at the end, it should show me
boundary =
0 0 5 4
blocks =
1 1 2 2
2 2 4 3
Thanks

Best Answer

fid = fopen('map1.txt');
boundary = fscanf(fid, 'boundary %f%f%f%f', 1);
blocks = cell2mat( textscan(fid, 'block %f%f%f%f', 'CollectOutput', true) );
fclose(fid);