How to stack .mat files?

Suppose in one of my data set, i have 20 .mat files.
Now, i want to stack all 20 .mat files together.
The issue is though they are in the same data set, size of each .mat file differs (not a huge variation though). As my final objective is to create a 3D model, how can i stack those .mat files together in order to open in another visualization software? Kindly (atleast) send me a link/documentation which help me to resolve this issue.

 Risposta accettata

Walter Roberson
Walter Roberson il 28 Apr 2016

1 voto

You cannot stack .m files. You might be able to stack variables that are stored in the files.
However, if the variables are not all the same size, you need to define how they are to align. Should they be rescaled (as if they are images of different resolution), or should the center all be in the same place and the smaller ones should be padded on all sides, or should the top left edge be aligned and the bottom right be padded on the smaller, or are these images containing features that have to be "registered" (lined up), or ... ?

5 Commenti

Chathu
Chathu il 28 Apr 2016
Hi Walter, thank you so much for your rapid response as usual. Really admire your great support:)
Since i want to reconstruct a 3D model i prefer to have the center to be in the same place and pad the rest of the data (because these data set have been already registered and segmented).
So i have a .mat file which consist of x,y,z in mm. Can i convert .mat to .jpg/.png and do the padding, is that what you meant? (kindly correct me, if i am wrong).
Another question, if i am using padding i think i should use 'PadArray' function. But i am not sure how to do the padding if i want to keep the image in the center and perform padding for the rest. Do you think you can guide me for that too?
dinfo = dir('*.mat');
data = [];
for K = 1 : length(dinfo)
thisfile = dinfo(K).name;
filestruct = load(thisfile);
%replace the following with a direct access into filestruct if the variable name is fixed
fn = fieldnames(filestruct);
var_to_load = fn{1};
thisvar = filestruct.(var_to_load);
[dr, dc, ~] = size(data);
[tr, tc] = size(thisvar);
%padding might be asymmetric
if dr < tr
npad1 = floor((tr - dr)/2);
npad2 = tr - dr - npad1;
data = [zeros(npad1, dc); data; zeros(npad2, dc)];
[dr, dc, ~] = size(data);
elseif dr > tr
npad1 = floor((dr - tr)/2);
npad2 = dr - tr - npad1;
thisvar = [zeros(npad1, tc); thisvar; zeros(npad2, tc)];
[tr, tc, ~] = size(thisvar);
end
if dc < tc
npad1 = floor((tc - dc)/2);
npad2 = tc - dc - npad1;
data = [zeros(dr, npad1), data, zeros(dr, npad2)];
[dr, dc, ~] = size(data);
elseif dr > tr
npad1 = floor((dc - tc)/2);
npad2 = dc - tc - npad1;
thisvar = [zeros(tr, npad1), thisvar, zeros(tr, npad2)];
[tr, tc, ~] = size(thisvar);
end
data(:,:,K) = thisvar;
end
Chathu
Chathu il 29 Apr 2016
Modificato: Chathu il 29 Apr 2016
Hi Walter, thank you so much for your rapid response.Really admire it.
i came up with couple of qu's:
(1) Getting the following error: Subscripted assignment dimension mismatch. Error in padding_exc_center (line 35) data(:,:,K) = thisvar;
(2) Since i am having about 20 mat files, i will perform the padding to the rest of files. But after doing so, is it possible to stack the output(eg:variable) so i can open in the other visualization s/w?
Thanks alot for your guidance so far. I am so grateful to you.
Chathu
Chathu il 29 Apr 2016
i am wondering why i am getting an error? (eg: Subscripted assignment dimension mismatch)
So its possible to stack the variable K now, is it? Thank you in advance.
Chathu
Chathu il 30 Apr 2016
i was able to get away with the error by commenting out the foll: data(:,:,K) = thisvar;

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Read, Write, and Modify Image in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by