image a set of matrix variables after loading them in
1 次查看(过去 30 天)
显示 更早的评论
Hello. I am trying to load a set of files in a folder into matlab, and then perform an operation on each of them. From the MATLAB FAQ I have the following code:
clear all; close all;
% Specify the folder where the files live.
myFolder = 'path for folder';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
load(fullFileName);
readmatrix(fullFileName);
end
When I run this it then loads all the files that are csv files from the folder into my matlap workspace, which is what I want. However, I need a way to perform an operation on all of them (in this case to image each file), without having to type out every file name. Is there an easy way to do this? I have tried adding in image(fullFileName) but this does not work as the image command must refer to a specific variable. Any help much appreciated!!
14 个评论
Adam Danz
2020-9-24
编辑:Adam Danz
2020-9-24
If the user hasn't made their email link public on their profile, they likely don't want to be contacted by community members outside of the forum. I include mine mainly for support of my files on the file exchange. Maybe restoring the deleted content would be the start of a compromise; I can understand their frustration with that.
采纳的回答
Stephen23
2020-9-16
Do not use load. Assign the imported data to an array, e.g.:
theFiles(k).data = readmatrix(fullFileName);
Now all of your imported data will be in the structure theFiles, which you can easily access using indexing:
2 个评论
Stephen23
2020-9-17
编辑:Stephen23
2020-9-17
"Is there a way to display each image from the image array at once?"
If you really want to you could create an array of figures and iterate over them, or use subplot, or create a video sequence, or something like that. I doubt that it would be very convenient, informative, or useful though.
"Or to save each image in the image array to my computer?"O
Of course, you can use imwrite.
"It seems to be only displaying the last image."
As you do not show your code, I have no idea how you are displaying these images. If you are displaying them in a loop, then quite likely on each loop iteration it will update the displayed image with the current image: all of them get get displayed, but only the last one will remain after the loop.
Rik
2020-9-18
编辑:Rik
2020-9-24
Now deleted comment:
Joe Joe on 16 Sep 2020 at 19:35
Thank you very much! This seems to be working, and I can use the arrayfun function to create an image array of all the data. Is there a way to display each image from the image array at once? Or to save each image in the image array to my computer? It seems to be only displaying the last image.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!