MATLAB Answers


Form a matrix from a structure

Asked by Andrew
on 4 Feb 2013

I have a 32*32 data structure. There are 1*1 structures in each of those 32*32 cells. However, i would like to extract all the binary data in the substructures and align them into one matrix. any one with some code that could help me do this please? Thank you


can you provide an example

on 4 Feb 2013

i am trying to write out something but just thought i needed help. this is what i have sofar. (2592,198) is the size of the matrix i would like to have my data in.(I hope this is what you meant by example)

file = zeros(2592,198); = load('Compressed.dat');
for i = 1:32;
    for j = 1:32;
        file = data(i,j);
        file(:) = {s};


No products are associated with this question.

3 Answers

Answer by Azzi Abdelmalek
on 4 Feb 2013
Edited by Azzi Abdelmalek
on 4 Feb 2013
 Accepted answer


x(1,2).data=[2 1];


Is your struct data like the above example?

out=struct2cell(x);  % x is your struct variable

What did you get?

on 4 Feb 2013

this is just part of the whole results. i dont want to crown the whole place with stuff so i just uploaded some of it but it all rather looks likes this. [1x90 char] '00000000100000000011000000010000000001111111110010' [1x90 char] [1x190 char] [1x310 char] [1x250 char] [1x150 char] [1x110 char] '0000000100000000010100000001100000010001000011001000000011111110100010' '0000000100000000010100000001100000000011000000111000000000111111100000' '00000001000000000101000000100000000011111111100010' [1x110 char] '0000000100000000010100000000100000000011000010001000000000111111010000' '0000000100000000010100000000100000000011000010001000000000111111010000' [1x110 char] [1x110 char] [1x110 char] [1x130 char] [1x130 char] [1x110 char] . . . .

Answer by Cedric Wannaz
on 4 Feb 2013

If it is a cell array and cells contents have matching sizes:

 >> doc cell2mat


Look at the figure in the doc of cell2mat. Sizes can vary, but they have to match between adjacent blocks. If your aren't in this situations, you'll have to define what how you want to manage gaps (e.g. fill them with NaNs so sizes match..?).

Seems that you edited the question since I posted my answer. Seeing your answer to Azzi, you should just look at what are these variables that you are dealing with, e.g.

 >> S = load('Compressed.dat') ;
 S = 
        ans: {...}
       data: {...}
 >> class(S)
 ans =
 >> class(
 ans =
 >> size(
 ans =
      32 32

... doing that would help you truly understand the structure of the objects that you are dealing with, which will allow you to index them correctly.

on 4 Feb 2013

So what i did was to use DCT to compress a 256*256 greyscale image using a 8*8 block matrix. the compressed data therefore comes out as a 32*32(256/8) data structure. i would like to align the extracted data and then the remaining gaps at the end of the matrix will be filled with zeros. Thats basically what i am doing. just in case you may want to look at the structure.

Answer by Andrew
on 4 Feb 2013

Thanks to Azzi and Cedric o was able to get a step forward. however my question now is, i have managed to extract all the binary digits from the structure and they are in a file. i would like to rearrange them into a matrix of 2592*2592. how do i do that? Below is my final code for extracting the binary digits(or bits??)

load('Compressed.mat'); %load compressed data
fid = fopen('saved_data.txt','w');
for i = 1:32;
    for j = 1:32


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!