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)
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:
%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: 22.214.171.1241785 (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
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');
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