# How to apply multiple parameters function on all the matrices in the folder?

조회 수: 1(최근 30일)
Ammy 2021년 9월 16일
편집: Image Analyst 2021년 9월 16일
A=randperm(16);
A=reshape(A,4,4);
function [B]=fun(A,a,b,c)
B= a.*A+b.*A+c.*A
end
vals = [1, 2,3
6,1,4
7,3,5] ;
[m,n] =size(vals) ;
B = zeros(4,4,m) ;
for i = 1:m
B(:,:,i)=fun(A,vals(i,1),vals(i,2),vals(i,3)) ;
end
The above code works perfectly for single A,
I want to apply the above on set of matrices stored in a foder
l have tried the following but it stored result only for single A
srcFiles = dir(...); % the folder in which matrices exists
for i = 1 : length(srcFiles)
filename = strcat('...',srcFiles(i).name);
vals = [1, 2,3
6,1,4
7,3,5] ;
[m,n] =size(vals) ;
B = zeros(4,4,m) ;
for j = 1:m
B(:,:,j)=fun(I,vals(j,1),vals(j,2),vals(j,3)) ;
% and want to save all the new B's .
end
end
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Awais Saeed 2021년 9월 16일
To store results for all matrices, use cell arrays. Something like this
for j = 1:1:m
Results{i,j} = B;
end

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

### 채택된 답변

Awais Saeed 2021년 9월 16일
2. Loop through them and find B
3. Store that B array to a cell
% path to the folder that contains .mat files
Folder_location = 'C:\Users\Awais\Documents\MATLAB';
% Get paths of all files in that folder
Files = fullfile(Folder_location, '*.mat');
Files = dir(Files);
% display File details
struct2table(Files)
for fileNum = 1:1:length(Files)
% get name of file
fileName = Files(fileNum).name;
% load file. It will be your matrix but in a struct
% Extract matrix from struct
fprintf('Loaded file # %d, name = %s \n',fileNum, fileName)
vals = [1 2 3; 6 1 4; 7 3 5];
[m ,n] = size(vals);
for j = 1:1:m
B = Myfun(A,vals(j,1),vals(j,2),vals(j,3));
Results{fileNum,j} = B;
end
end
function [B] = Myfun(A,a,b,c)
% A is a matrix here
B = a.*A + b.*A + c.*A
end
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Awais Saeed 2021년 9월 16일
dir() returns a struct. I used struct2table() to display what you got from dir() (look in the attached image). Secondly, I am not well aware how to deal with images and I cannot help you with that but you can get the idea how to load an image from the snippet provided below:
% path to the folder that contains .png files
Folder_location = 'C:\Users\Awais\Documents\MATLAB';
% Get paths of all files in that folder
Files = fullfile(Folder_location, '*.png');
Files = dir(Files);
% display File details
struct2table(Files)
for fileNum = 1:1:length(Files)
fileName = Files(fileNum).name;
fprintf('Loaded file # %d, name = %s \n',fileNum, fileName)
fprintf('Size of %s is: ', fileName)
fprintf('\n')
end
Also you are using
A=randperm(16);
A=reshape(A,4,4);
in your question. However, converting an image to matrix will give you a huge matrix or most probably a multi-dimensional array which is still bigger than 4x4 matrix (look the size of images I got in the attached image).

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

### Community Treasure Hunt

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

Start Hunting!

Translated by