90 views (last 30 days)

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!!

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
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.

Opportunities for recent engineering grads.

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

Start Hunting!
## 14 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1013776

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1013776

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014061

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014061

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014574

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014574

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014772

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014772

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014946

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1014946

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023082

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023082

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023109

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023109

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023199

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023199

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023247

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023247

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023289

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023289

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023316

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023316

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023319

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023319

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023331

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023331

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023340

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/594904-image-a-set-of-matrix-variables-after-loading-them-in#comment_1023340

Sign in to comment.