MATLAB Answers

agatte
0

display .mat file as an image ?

Asked by agatte
on 6 Dec 2011
Latest activity Answered by manasa p
on 9 Feb 2018
Hi All Matlab users,
I have an image in .mat file ... I have a problem with display it.
clear all;
I=load ('Data.mat'); % struct double 512x512
I=I.s.I;
I=double(I);
figure(1);
imshow(I);
Could someone help me please ?
I would appreciate for any help.

  2 Comments

Jan
on 6 Dec 2011
Beside the useless "clear all" (see http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301), what is the problem? Do you get an error message - if so, which one?
Image Analyst
on 6 Dec 2011
Help with what? It's possible that could work if you had a variable called s, that was a structure and that had a member called I which was an array. Did you get an error or something? What does your save() function call look like - what you used when you saved "s" into Data.mat?

Sign in to comment.

5 Answers

Answer by Image Analyst
on 6 Dec 2011
 Accepted Answer

Why don't you just do something like
% Load entire mat file contents into a structure.
% The structure has a member "I" that is a double 512x512 array.
storedStructure = load('Data.mat');
% Extract out the image from the structure into its own variable.
% Don't use I as the variable name - bad for several reasons.
imageArray = storedStructure.s.I; % Or storedStructure.I depending on what members your structure has.
% Display the image in a brand new figure window.
figure(1);
% Display double image, scaled appropriately.
% No need to cast to uint8 - could even be bad
% if your double numbers don't span the 0-255 range nicely.
imshow(imageArray, []);

  4 Comments

Show 1 older comment
Image Analyst
on 1 Sep 2015
S is the structure that the original user saved when he called save() to create the mat file. And s has a field (member) called I. Those are not good descriptive names, as I pointed out to him, and as you now demonstrate, but those were his choices, not mine. I hate single letter variable names, except for loop indexes, because they lead to an alphabet soup of code that is hard to follow, understand, and maintain.
When I trying this code for display a mat file it shows
" Reference to non-existent field 's'. imageArray = storedStructure.s.I; "
What is the problem with my code?
Image Analyst
on 10 Dec 2017
Your mat file does not have a variable in it called s. Look at storedStructure in the variable editor to see what fields it has. Or do one of these lines:
storedStructure = load(filename) % Don't use a semicolon!!!
or
fieldnames(storedStructure) % Don't use a semicolon!!!

Sign in to comment.


Answer by agatte
on 6 Dec 2011

It is not good . I received en error : ??? Error using ==> uint8 Conversion to uint8 from struct is not possible.
I = load('Data.mat'); figure(1); imshow(uint8(I));
????

  1 Comment

Image Analyst
on 6 Dec 2011
Chandra's version did not accept the output of load() into a variable, like you did. In your code I would be a structure. Do what he did and leave off the I= so that "s" gets loaded into the function's workspace, then get the "I" member of "s" like you originally tried to do. But you never said what *your* code's error was, as Jan and I asked.

Sign in to comment.


Answer by Dark
on 9 Apr 2014

How do I display 512x512x5 .mat file as an image in Matlab ?

  4 Comments

Show 1 older comment
Dark
on 9 Apr 2014
My matFileName is volume_image.mat. I tried your steps but the 2nd line gives me an error - Reference to non-existent field 'rgbImage'.
Image Analyst
on 9 Apr 2014
Dark, you're supposed to know the name of the variable you used, and replace rgbImage with the actual name. If you don't remember just put storedStructure on its own line and it will report its fields to the command window:
storedStructure % Report fields to command window.
You will see one of the fields look like an image. Use the name of that one to replace rgbImage in my code.
Dark
on 9 Apr 2014
Thanks for your quick reply. I followed your code and get an image. However the image that I get using imshow() is just black and white. I even tried using imshow(rgbImage, []). I basically have 5 dicom slices of a bone which I converted into .mat file. Then I followed your code. How do I view the 5 dicom images in Matlab ?

Sign in to comment.


Answer by Annie micheal on 1 Sep 2016

How to convert 100 face images to .mat file of the form 20*20*100 where 20*20 is the face template and 100 is the number of images.

  8 Comments

Image Analyst
on 3 Sep 2016
Well, give it a try.
Its working.. thank u..

Sign in to comment.


Answer by manasa p
on 9 Feb 2018

how to display 83*86*220 data set image in mat tool

  0 Comments

Sign in to comment.