How can I plot a histogram for each of the multiple .mat files located in a folder outside of MATLAB?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Kevin Holly
2021-9-29
编辑:Kevin Holly
2021-9-29
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 个评论
Image Analyst
2021-9-29
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.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!