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

2 ビュー (過去 30 日間)
Ammy
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);
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 件のコメント
Awais Saeed
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 件のコメント
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by