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 个评论
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
2021-9-16
- Read your .mat files
- Loop through them and find B
- 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
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 Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!