Extracting values from incomplete matrix set

조회 수: 6 (최근 30일)
MiauMiau
MiauMiau 2015년 9월 8일
편집: Thorsten 2015년 9월 8일
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?

답변 (1개)

Thorsten
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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by