Extracting values from incomplete matrix set

1 view (last 30 days)
MiauMiau
MiauMiau on 8 Sep 2015
Edited: Thorsten on 8 Sep 2015
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?

Answers (1)

Thorsten
Thorsten on 8 Sep 2015
Edited: Thorsten on 8 Sep 2015
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

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!