save matrix with unique subject ID to new file

4 views (last 30 days)
Hi all,
This is my first time posting a q so sincere apologies if I make a mathworks forum faux pas.
I'm doing research that involves unique data per subject (e.g., behavioral research), and analysis yielding a "results matrix" for each subject. Afterward I'd like to concatenate results matrices such that it adds another dimension to 1 big array. To be more specific, the results matrices I have now (running the script 1 subject at a time) are size 360x360x64 (this is the data for 1 subject). Later, once I have n=10 subjects "run", I'd like to have a full array of size 360x360x64x10.
So, is there a way to rename the results matrix variable dynamically, so that its something like resultsMatrixSubj01 = [360x360x64 data goes here], save that .mat file, and move it out of the main directory. Then, recall all of those subject matrices to be concatenated?
Right now I can save the file with a unique filename, and move it (using movefile), but once you re-load the filename into the workspace, it has the original variable name, which is not unique - so I'm worried I won't be able to concatenate later on because all subjects will have the same variable name???
EX of what I have now:
...script runs and results in a 360x360x64 matrix variable called resultsMatrix
subjID = {'01'}; % this is an input I have already in the script
newID = cell2mat(subjID);
fileName = ['resultsMatrixSubj' newID '.mat'];
save(fileName, 'resultsMatrix');
moveFile(fileName, '~/newDirInfoHere/);
This is all fine, but then when I do: load('resultsMatrixSubj01.mat'), the workspace variable is simply called resultsMatrix. If I do this for all 10 subjects later on (to concatenate), they'll all have the same variable name in the workspace, even if they have unique file names?
Something I'm thinking about, but can't quite sort out how to do, is to create a structure, where each field is a different subject's results (maybe something like resultsMatrices.(subjectID), and save in a directory set up to be a repository just for these results. But could I do this where I "add to" (or grow) the structure by running the script separately each time and saving with maybe some sort of -append tag? I really don't want to write a for loop because I worked hard to re-write old code a coworker gave me that had a crazy nested for-loop that took forever, and I vectorized it (trade-off being I have to run subjects separately - not too big of a deal because I like to inspect the data every time as these are large fMRI connectivity matrices and I'd like to make sure the results aren't funky). That being said, I'll loop if you all think it's the best idea!
Sorry for the long message, I want to be as clear as possible, and I know dynamically creating variable names and saving is frowned upon, but my logic is that if I'm moving it out of my main directory then it's ok? Any advice would be great! Thanks!
Regards, Carrisa

Answers (1)

Matt J
Matt J on 19 Feb 2017
Edited: Matt J on 19 Feb 2017
This is all fine, but then when I do: load('resultsMatrixSubj01.mat'), the workspace variable is simply called resultsMatrix. If I do this for all 10 subjects later on (to concatenate), they'll all have the same variable name in the workspace, even if they have unique file names?
This implies that you are already doing some sort of loop over file names. To reload them in a way that can be distinctly accessed, you could do something like this,
clear S
for i=10:-1:1
S(i)=load(['resultsMatrixSubj' num2str(i,'%.2d')]);
end
TotalConcatenation= cat(4, S.resultsMatrix);
However, part of me wonders if the only reason you are saving the data to 10 separate .mat files is for the purpose of the concatenation? If so, it is unnecessary gymnastics. You could be doing something like the following, without ever saving the data subject-by-subject,
C=cell(1,10);
for i=1:10
C{i}=getResultsMatrix(i);
end
TotalConcatenation=cat(4,C{:});
or better yet,
TotalConcatenation=nan(360,360,64,10);
for i=1:10
TotalConcatenation(:,:,:,i)=getResultsMatrix(i);
end
  1 Comment
Carrisa Cocuzza
Carrisa Cocuzza on 19 Feb 2017
Hi, Thanks for the quick reply!
So I'm running an analysis script 1 subject at a time, sorry I re-read my original comment and realize it was a little confusing! The reason for this is that each subjects' data is transformed in previous analyses in such a way that matrix sizes (to the point of being ready to be an input for the analysis of interest here) are not always the same ("pre-processing" steps for dealing with fMRI data, if you're familiar - final time series are not always equal once nuisance variables are identified an regressed out).
So instead of looping over subject numbers (as it was in a previous version of the script), I'm running this bit of the analysis 1 subject at a time, which results in a 360x360x64 array, per subject (e.g., the 360x360x64 array is 1 subject's analyzed data). However, because the script for this portion of the analysis is always the same, the results array is always the same variable name. So if I run it subject by subject, I can change a filename but I'm having trouble making unique/dynamic variable names.
But I'm thinking that I'll need to deal with the different size issue and just loop over subjects and implement code like what you've suggested - I'll try it out and update for other interested readers. Thanks! -Carrisa

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!