How do I display the workspace variable file name via MATLAB script?

2 ビュー (過去 30 日間)
bobby
bobby 2021 年 9 月 30 日
コメント済み: bobby 2021 年 9 月 30 日
What line of code would display the workspace variable file name (so in this case it would be a .mat file) being used?
My goal is to be able to have the file name match up its respective plotted histogram. Said code would be a part of an already existing script that is analyzing multiple .mat files in a for loop.
Thank you all who respond and help!
  2 件のコメント
Kevin Holly
Kevin Holly 2021 年 9 月 30 日
Hey bobby,
I'm assuming you are working on the same project from yesterday.
We obtained a list of all the files in a directory.
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
To access the file names, you can use the following command:
files.name
If you want to view the first file on the list:
files(1).name
Did you want to place the file name on the histogram's title? Please let me know what you need.
%let's analyze each file
for i = 1:length(files)
Data = load(fullfile(files(i).folder, files(i).name))
histogram(Data.IntensityImagedataCh1)
drawnow;
xlabel('X label')
ylabel('Y label')
title(['A histogram of ' strrep(files(i).name,'.mat','')])
saveas(gcf,fullfile(save_directory,strrep(files(i).name,'.mat','.fig')),'fig');
end
Does each .mat file you are working with save the matrix under the variable name IntensityImagedataCh1?
bobby
bobby 2021 年 9 月 30 日
編集済み: bobby 2021 年 9 月 30 日
Having the file name being shown in the histogram name would certainly suit me!
And yes, each .mat file in the loop would save the matrix under the variable name "IntensityImagedataCh1". Would that impact how the code is written?

サインインしてコメントする。

採用された回答

Kevin Holly
Kevin Holly 2021 年 9 月 30 日
編集済み: Kevin Holly 2021 年 9 月 30 日
"And yes, each .mat file in the loop would save the matrix under the variable name "IntensityImagedataCh1". Would that impact how the code is written?"
Yes, it would. This actually simplifies the problem.
We first load the data as a structure here:
Data = load(fullfile(files(i).folder, files(i).name))
This structure contains subfields. In your case, the subfield is named "IntensityImagedataCh1" where it contains a matrix.
We can access this subfield by using a period:
Data.IntensityImagedataCh1
Since the subfield is the same between files, we do not need to change this line in code that accesses the matrix.
So, this should work for you:
%let's analyze each file
for i = 1:length(files)
Data = load(fullfile(files(i).folder, files(i).name))
histogram(Data.IntensityImagedataCh1)
drawnow;
xlabel('X label')
ylabel('Y label')
title(['A histogram of ' strrep(files(i).name,'.mat','')])
saveas(gcf,fullfile(save_directory,strrep(files(i).name,'.mat','.fig')),'fig');
end
The above should save the the filename in the title of the histogram and insert the name in the saved figure's filename.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by