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

1 vue (au cours des 30 derniers jours)
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 commentaires
Ammy
Ammy le 16 Sep 2021
Modifié(e) : Image Analyst le 16 Sep 2021
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 le 16 Sep 2021
Modifié(e) : Awais Saeed le 16 Sep 2021
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

Connectez-vous pour commenter.

Réponse acceptée

Awais Saeed
Awais Saeed le 16 Sep 2021
  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 commentaires
Ammy
Ammy le 16 Sep 2021
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 le 16 Sep 2021
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).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by