image a set of matrix variables after loading them in

조회 수: 2 (최근 30일)
Joe Joe
Joe Joe 2020년 9월 16일
편집: Ashish 2020년 9월 25일
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
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.
Joe Joe
Joe Joe 2020년 9월 24일
편집: Joe Joe 2020년 9월 24일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
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
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
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by