image a set of matrix variables after loading them in
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joe Joe
el 16 de Sept. de 2020
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 comentarios
Adam Danz
el 24 de Sept. de 2020
Editada: Adam Danz
el 24 de Sept. de 2020
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.
Respuesta aceptada
Stephen23
el 16 de Sept. de 2020
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 comentarios
Stephen23
el 17 de Sept. de 2020
Editada: Stephen23
el 17 de Sept. de 2020
"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
el 18 de Sept. de 2020
Editada: Rik
el 24 de Sept. de 2020
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!