MATLAB Answers

image a set of matrix variables after loading them in

90 views (last 30 days)
Joe Joe
Joe Joe on 16 Sep 2020
Edited: Ashish Pasham on 25 Sep 2020 at 0:10
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);
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
% 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);
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!!


Joe Joe
Joe Joe on 24 Sep 2020 at 21:54
Adam, I see. Thank you for your help. Are there any steps I can take to get in contact with the MVPs directly rather than commenting on this post? Or are there any steps I can take to convince the various MVPs who posted my name to remove it? I am kind of lost on how to proceed with this, as I am unable to get a response from Stephen.
Adam Danz
Adam Danz on 24 Sep 2020 at 22:06
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.
Joe Joe
Joe Joe on 24 Sep 2020 at 22:19
Adam, I have restored the deleted content (at least all related to the question, I deleted the old comments I had that were asking to remove my name earlier, just because they weren't pertinent to the question, I don't know if there is a way to restore these or not, or if they would be worth restoring). Is there anyway I can get the attention of the MVPs who mentioned my name? Because as far as I can tell unless they subscribed to get emails about this question then there is no way for them to notice my request to have my name removed. I have tried flagging their comments but either this doesn't notify them or just made them upset. Or is there a way that you as an MVP could contact them?
edit: the reason I want to contact them is just so I can explain such as how I have done with you why I want my name removed, and see if there's a compromise that works.

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 16 Sep 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:


Stephen Cobeldick
Stephen Cobeldick on 17 Sep 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 on 18 Sep 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.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!