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

조회 수: 2 (최근 30일)
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);
I = %reading(filename);
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
Ammy
Ammy 2021년 9월 16일
편집: Image Analyst 2021년 9월 16일
In other words, I want to find the following for 100 A's that are stored in a folder
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
Yes, of course, I need B corresponding to every A in the folder.
Awais Saeed
Awais Saeed 2021년 9월 16일
편집: Awais Saeed 2021년 9월 16일
To store results for all matrices, use cell arrays. Something like this
for j = 1:1:m
B = fun(FileLoaded,vals(j,1),vals(j,2),vals(j,3));
Results{i,j} = B;
end

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

채택된 답변

Awais Saeed
Awais Saeed 2021년 9월 16일
  1. Read your .mat files
  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
FileLoaded = load(fileName);
% Extract matrix from struct
A = cell2mat(struct2cell(FileLoaded))
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
Ammy
Ammy 2021년 9월 16일
Thank you very much. If I have A as image then I got the error
Undefined function 'struct2table' for input arguments of type 'struct'.
Is there way to resolve this , in other words if there are images in a folder then?
Awais Saeed
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)
% read image
FileLoaded = imread(fileName);
fprintf('Size of %s is: ', fileName)
size(FileLoaded)
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).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by