How can I plot a histogram for each of the multiple .mat files located in a folder outside of MATLAB?

8 views (last 30 days)
bobby
bobby on 29 Sep 2021
Commented: bobby on 29 Sep 2021
I am new to using MATLAB to analyze and visualize many individual files of data.
Here is the situation: I have multiple (20+ but can certainly extend to the 50-60+ range) .mat files that are typically (but not always) in the form of a 1024x1024 matrix. These .mat files are automatically placed in a folder outside of MATLAB (the folder's location is specified as the save file path in a seperate script). Each of those codes share a common naming tag (ie. an identifying text that can be used to readily identify the targeted .mat files).
I would like to be able to produce a reusable code that can take all of the selected .mat files in that filepath and produce a histogram for each .mat at the same time. The values of the matricies can differ so I wouldn't want to put any restraints on axises or rows/columns.
The code I have so far is as follows:
clear all
%The Code below specifies the filepath on my computer where said .mat files are located in
directory = '/Users/NAME/Desktop/selectedmatricies/';
realdata = dir(directory);
%MATLAB Version: 9.7.0.1261785 (R2019b) Update 3
I am unsure where to go from here, or even if the code I listed above is even needed for what I am intending. I do have some suspiscion that a for loop would play a role in the code that would be generated, but I do not know how to structure it.
I have scoured MathWorks for previously asked questions in a similiar predicament to mine, however my efforts were fruitless. I am very much open to any ideas or codes that the community can provide! Thank you so much everyone!
TLDR; I would like to take multiple .mat files in an outside folder and run them through MATLAB code to produce a histogram per file

Accepted Answer

Kevin Holly
Kevin Holly on 29 Sep 2021
Edited: Kevin Holly on 29 Sep 2021
Here is one method you can use:
directory = '/Users/NAME/Desktop/selectedmatricies/'; %Alternatively, could use: directory = uigetdir()
files=dir(fullfile(directory,'*.mat')); % This gets all the .mat files in the directory. * is a wild card
save_directory=uigetdir('C:\'); % Where you want to save your histograms
%let's analyze each file
for i = 1:length(files)
matrix = load([file(i).folder,filesep,file(i).name],'-mat');
histogram(matrix)
xlabel('X label')
ylabel('Y label')
title('title')
saveas(gcf,[save_directory,filesep,strrep(file(i).name,'.mat','.fig')],'fig'); %save figure (can also save as an image (e.g. .png or .tif))
% Could use the line below if you want to save the figure with the word histogram
%saveas(gcf,[save_directory,filesep,strrep(file(i).name,'.mat','_histogram.fig')],'fig'); %save figure
end
  17 Comments
bobby
bobby on 29 Sep 2021
@Kevin Holly and @Image Analyst, thank you for sticking with me through this issue and lending your expert suggestions! From your guidance and expertise, I was able to figure out the code that would be able to work with my current script! Thank you both so very much!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Sep 2021
The answer to this and lots of other good stuff can be found in the FAQ:
  1 Comment
bobby
bobby on 29 Sep 2021
Thank you! I see that the first example would be most relevant but how would I change that code to analyze .mat files into histograms, rather than it analyzing a group of .png files as seen in the example? Would the code still be the same?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!