How can I plot a histogram for each of the multiple .mat files located in a folder outside of MATLAB?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Kevin Holly
le 29 Sep 2021
Modifié(e) : Kevin Holly
le 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 commentaires
Image Analyst
le 29 Sep 2021
I'd just add
drawnow;
after histogram() to make sure it paints the screen every time. Otherwise you might just see the latest histogram only.
Plus de réponses (1)
Image Analyst
le 29 Sep 2021
The answer to this and lots of other good stuff can be found in the FAQ:
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!