Extracting values from incomplete matrix set
1 次查看(过去 30 天)
显示 更早的评论
Hi So despite the cumbersome question title, the problem is simple: I want to extract the value (62,122) from 84 matrices. The matrices are contained in .mat files named something like transformed_group1_subj1.mat or transformed_group1_subj88.mat. The problem is the following: Not each subject number is contained in the folder. For instance, I have no transformed_group1_subj8.mat, such that my following code throws an error:
for k = 1:2
for j = 1:155
load(['transformed_group' num2str(k) '_subj' num2str(j) '.mat']);
if k == 1
rank1(j) = fisher_matrix(62,122);
% not all subject number from 1 to 155 exist, throw an exception?
elseif k == 2
rank1(j+155) = fisher_matrix(62,122);
end
end
end
error:
>> analyzeRanks Error using load Unable to read file 'transformed_group1_subj8.mat': no such file or directory.
Error in analyzeRanks (line 6) load(['transformed_group' num2str(k) '_subj' num2str(j) '.mat']);
what can I do?
0 个评论
回答(1 个)
Thorsten
2015-9-8
编辑:Thorsten
2015-9-8
Just check if the file exist before loading
rank1 = nan(155, 2); % preallocate for speed
for k = 1:2
for j = 1:155
filename = ['transformed_group' num2str(k) '_subj' num2str(j) '.mat'];
if isfile(filename)
load(filename)
rank1(j,k) = fisher_matrix(62, 122);
end
end
end
% reshape rank1 from 155 x 2 to to 2*155 x 1 vector if this format is more handy for you
rank1 = rank1(:);
You need the convenience function
function yn = isfile(str)
yn = exist(str, 'file') == 2;
or you just write
if exist(filename, 'file') == 2
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!