MATLAB: extracting onsets in different files and saving them in different files
My MATLAB script is to:
Extract four different fMRI onsets from MATLAB files (the files are named 'subject 06 data', 'subject 05 data', etc.)
Put this information in a new file with two other variables named 'durations' and 'names'.
Save all this as a new MATLAB file.
I am facing two problems:
At the moment, the script below manages to do steps 1 through 3 for the first MATLAB file in the directory 'Gender_recogntion', but it does not do 1 through 3 for the other MATLAB files in the folder. It crashes in the loop at the line 'load(sub_name(i).name);'.
This is the error I get:
??? Improper index matrix reference.
Error in ==> Gender_onsets_script_2 at 16
load(sub_name(i).name);
In addtion, I would like to name the new MATLAB files with the name of the original MATLAB files. At the moment, the new MATLAB files is named 'onsets.mat'.
clear all
close all
clc
cd 'C:\Program开发者_JAVA百科 Files\MATLAB\R2007b\Data\Resilience\Real_data\Raw\Matlab_files\Gender_recogntion';
sub_name = dir('C:\Program Files\MATLAB\R2007b\Data\Resilience\Real_data\Raw\Matlab_files\Gender_recogntion\*.mat');
for i = 1:numel(sub_name);
load(sub_name(i).name);
names = {'sad' 'anger' 'neutral' 'rest'};
durations = {[18] [18] [18] [18]};
onsets=cell(1,4);
onsets{1} = data.time_since_scan_start(data.emotion==5)/1000; %Get the 36 onsets for sad.
onsets{2} = data.time_since_scan_start(data.emotion==4)/1000; %Get the 36 onsets for anger.
onsets{3} = data.time_since_scan_start(data.emotion==6)/1000;% Get the 36 onsets for calm.
onsets{4} = datarest.onset/1000; %Get the six onsets for the rest blocks.
onsets{1} = onsets{1}(1:6:36)'; %Get the first onset value of each of the six blocks.
onsets{2} = onsets{2}(1:6:36)';
onsets{3} = onsets{3}(1:6:36)';
onsets{4} = onsets{4}';
%cd Onsets folder, saves onsets, and then cd back to folder "Matlab_files"
cd 'C:\Program Files\MATLAB\R2007b\Data\Resilience\Real_data\Onsets';
save 'onsets.mat' names durations onsets
cd 'C:\Program Files\MATLAB\R2007b\Data\Resilience\Real_data\Raw\Matlab_files\Gender_recogntion';
end
For your second question about naming the output files the same as the input, you can use the function version of save and pass in the variable sub_name(i).name as the filename argument.
save(sub_name(i).name, 'names', 'durations', 'onsets')
This uses the exact same name for input and output (in different directories, in your script). When I save output files, I typically keep them in the same directory as the inputs, so I modify an input filename with regular expressions (see regexprep) or adding a prefix or suffix (strcat) to create a related but distinct output filename.
For future reference...the default filetype for save is MATLAB data format; you could pass in '-ASCII' as an argument to save as a text file if your data types were compatible. The cell arrays in this example aren't, but strings and numerical matrices would be, so if text output files were important, you could use alternate data structures from the beginning or convert cells with cell2mat. A generic example with the save() version: save(filename, '-ASCII', 'x', 'y','z') where x,y,z are ASCII-friendly variables and filename is a text file.
[additional response, adding Jan 5, 2011]
About your first question on the error message: ??? Improper index matrix reference.
Is it possible that a saved .mat file contains a variable named dir, that would override the standard directory-listing function and cause that error? I read that tip on another site, just wanted to pass it along in case it helps.
精彩评论