How dispaly .mat as an image, then save it, then crop the saved image from the center ?

4 views (last 30 days)
Mohamed Elbeialy
Mohamed Elbeialy on 23 Nov 2020
Commented: Mohamed Elbeialy on 25 Nov 2020
-looking to dispaly multi (.mat) files as images
-save or crop the center then save the cropped images it
Image Analyst
Image Analyst on 24 Nov 2020
If the help in my Answer below did not work for you, then attach one of the mat files with the paperclip icon and tell us what part of the center you want to save and what you'd like the output filename to be.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 24 Nov 2020
Use the FAQ to read in a sequence of lots of .mat files.
In the loop, get your image from the mat file then crop it. Here's a start.
% Specify the folder where the files live.
myFolder = pwd; % or wherever, like 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
s = load(fullFileName);
% See if this structure has a field called myImage or whatever it's called in your programs.
if isfield(s, 'myImage')
imageArray = imread(fullFileName);
imageArray = imageArray(row1:row2, col1:col2, :);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
fprintf('Displaying myImage from %s\n', baseFileName);
fprintf(' myImage not found in %s\n', baseFileName);
Of course you need to assign row1, row2, col1, and col2 according to how you'd like to do the cropping.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!