REST functional connectivity error 'Matirix dimensions'

Hi, REST exparts

 I'm trying to analyze my resting state fMRI data using by REST functional connectivity. I've tried many times, but I always encount this error.

Exception occured
Error using ==>eq
Matrix dimensions must agree

I think that dimensions of my data don't match. Dimensions of my data are 79X95X69(Vox size 2X2X2, smooth 6mm). How can I solve this probrem? I'm sorry I'm just a biginner.

 Thanks in advance.
Sincerely,
 Yuki

Not enough information you have given.

You may attach REST running log file named "rest.log" in your REST directory after you encounter some problems.
This file logs the Matlab commands' output and may help us locate the error position.
This is a most detailed information. So to reduce the read time of this log, you may just Open Matlab -> Open REST -> Repeat your Error -> Close REST -> Find this log and Report here. Actually, only the last exception lines works, the other log lines helps us know the input and processing informations.

Another option is to describe your input and your steps in more details. And put the full exception information here, only "Error using ==>eq" is used to show Matlab's complaint not REST's and is not enough.

After all, REST is only a tool, so keep in mind what you are operating matters and helps you solve problems, I think.

Dear, REST exparts
 I'm sorry I didn't write enough information.

Data acquisition(resting state without any kind of task)

 Philips 1.5T MR scanner, TR 2411, TE 40,
 Flip angle 80, FOV 64*64, Matrix 64*64, Voxel size 3.0*3.0*3.6(gap=0), 35slices
Preprosessing(SPM5)
 realign to first image
 normalize to EPI template
 smooth FWHM 6mm
Nifti>>Analyze
  using Debabeler

Then I discard 5 volumes and use Resting-state fMRI data analysis toolkit V1.3 .  For example, followings are the error logs I 've encountered when I put one healthy control resting state fMRI data.

When I use 'default mask'
Removing the linear trend:

Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze"........................................
.......................................

Ideal rectangular filter: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend"
Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend"........................................

Load mask "Default".
Exception occured. ()
Error using ==> rest_ReadNiftiImage at 103
Meet error while reading the data
103#line, rest_ReadNiftiImage, in "/Applications/setup/REST/REST2007V1.3_090422/rest_ReadNiftiImage.m"
61#line, rest_readfile, in "/Applications/setup/REST/REST2007V1.3_090422/rest_readfile.m"
29#line, rest_loadmask, in "/Applications/setup/REST/REST2007V1.3_090422/rest_loadmask.m"
61#line, rest_bandpass, in "/Applications/setup/REST/REST2007V1.3_090422/rest_bandpass.m"
1190#line, BandPass, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
1286#line, btnComputeFC_Callback, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
96#line, gui_mainfcn, in "/Applications/MATLAB_R2007b/toolbox/matlab/guide/gui_mainfcn.m"
35#line, fc_gui, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
>>

When I use 'no mask'
REST Version: , Release:

Removing the linear trend:

Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze"........................................
.......................................

Ideal rectangular filter: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend"
Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend"........................................

Load mask "".
Build band pass filtered mask. Wait...
Band Pass Filter working. Wait.......................
ReConstructing 3D+time Dataset. Wait.......................
Saving filtered images. Wait..........................................
Band pass filter over.
Elapsed time is 242.402859 seconds.

Functional connectivity :"/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend_filtered"

Computing functional connectivity with: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend_filtered"
Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend_filtered"........................................

ROI time course retrieving through "ROI Center(mm)=(-4, -52, 22); Radius=4.00 mm.".

Seed ROI Definition: ROI Center(mm)=(-4, -52, 22); Radius=4.00 mm.
Brain Size: (79 95 69), Voxel Size: (2 2 2), Origin: (0 0 0)
Contained Voxel count: 0

Exception occured. (MATLAB:dimagree)
Error using ==> eq
Matrix dimensions must agree.
81#line, rest_writefile, in "/Applications/setup/REST/REST2007V1.3_090422/rest_writefile.m"
112#line, rest_SphereROI, in "/Applications/setup/REST/REST2007V1.3_090422/rest_SphereROI.m"
126#line, fc, in "/Applications/setup/REST/REST2007V1.3_090422/fc.m"
1323#line, btnComputeFC_Callback, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
96#line, gui_mainfcn, in "/Applications/MATLAB_R2007b/toolbox/matlab/guide/gui_mainfcn.m"
35#line, fc_gui, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
>>

 I realize 'REST' is only a tool, but I can't get detailed information about analizing steps of this tool from internet, one's manual and references.

 Thanks in advance.
Sincerely,
 Yuki

Hi, Yuki:

Thanks for your interest of REST and you gave a good try. The English manual could be downloaded here.

For your first problem, it seems that after you pressed "Compute"  functional connectivity button, there is some error with the default mask. Chaogan Yan or Xiangyu Long may give some analysis since one author of the masks contained in REST is Chaogan Yan. I think in princple, the mask's dimension should match with your data or you may have to build your own mask.

For your second question, the exceptions show that the origin of your EPI images was not set correctly. You could use some tools to set the origin of your EPI datas such as MRIcro or REST's "rest_ChangeOrigin_Recursive" or "rest_ChangeOrigin" which only works with ANALYZE 7.5 format EPI data. "rest_ChangeOrigin" need two parameters: one is a data dir only containing a series of ANALYZE 7.5 format EPI data, another should be the origin such as [31 43 25] for dimension [61 73 61]. 

The origin is very important for functional connectivity analysis with REST especially when you defined a seed ROI with REST. And you should carefully check the ROI definition with REST's Slice Viewer to make sure the ROI is the correct position you want to define.

Good luck,
Xiaowei Song

Dear,Xiaowei Song

Thank you very much for your kind reply.

first question
 I also think I have to match dimensions with my data. The English manual said four types of default masks in REAT tool.
  Voxel size=3*3*3, Dimensions=61*73*61
  Voxel size=3*3*3, Dimensions=53*63*64
  Voxel size=2*2*2, Dimensions=79*95*69
  Voxel size=2*2*2, Dimensions=91*109*91
I think I can choose one of these masks, but I can't get the information how to change dimensions of default masks.

second question
My EPI data information is following.
 Voxel size=2*2*2, Dimensions=79*95*69
 Origin 40,48,35
Is this origin different from one of REST setting? Which origin is appropriate for my data?

 Thanks in advance.
Sincerely,
 Yuki

  1. The manual I gave is about REST v1.1. And REST v1.3 changd some. The mask is one of the changes. You may check default masks in mask dir under REST's directory. A quick and dirty way to resample a mask could be found at http://restfmri.net/forum/node/78.
  2. From the log lines:
Seed ROI Definition: ROI Center(mm)=(-4, -52, 22); Radius=4.00 mm.
Brain Size: (79 95 69), Voxel Size: (2 2 2), Origin: (0 0 0)
Contained Voxel count: 0

I guess the origin (0 0 0) seems not defined. If all your EPI data preprocessed by SPM5 already set with Origin (40,48,35), it would be weird because REST does not have any default setting of origin and only supposes the origin has been set correcttly.

1.
 Of course, I've already check those up before I use this mail. I've found 6 types of brain mask. I've already done analysis filling  'User's defined mask' with 'BrainMask_05_79x95x69'. When I did so, I encountered following error. Maybe 'default mask' is Nifti format....

REST Version: , Release:

Removing the linear trend:

     Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze"........................................
.......................................

Ideal rectangular filter:    "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend"
     Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_026_analyze_detrend"........................................

     Load mask "/Applications/setup/REST/REST2007V1.3_090422/mask/BrainMask_05_79x95x69.img".
Exception occured.    ()
    Error using ==> rest_ReadNiftiImage at 103
Meet error while reading the data
    103#line,        rest_ReadNiftiImage,    in "/Applications/setup/REST/REST2007V1.3_090422/rest_ReadNiftiImage.m"
    61#line,        rest_readfile,    in "/Applications/setup/REST/REST2007V1.3_090422/rest_readfile.m"
    43#line,        rest_loadmask,    in "/Applications/setup/REST/REST2007V1.3_090422/rest_loadmask.m"
    61#line,        rest_bandpass,    in "/Applications/setup/REST/REST2007V1.3_090422/rest_bandpass.m"
    1190#line,        BandPass,    in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
    1286#line,        btnComputeFC_Callback,    in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
    96#line,        gui_mainfcn,    in "/Applications/MATLAB_R2007b/toolbox/matlab/guide/gui_mainfcn.m"
    35#line,        fc_gui,    in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
>>

 The way to resample mask you suggested may be very helpful to me.., but you mentioned 'quick and dirty' way.  I can't get precise meaning you say.. Do you mean it's not recommended way?

2. I do all of preprocessing steps by SPM5 and I check all of preprocessed data by 'Display'(SPM5 function). So, I think I've succeeded in setting origin. Is this also error in matching dimensions??

Thanks in advance.
Sincerely,
 Yuki

  The way to resample mask you suggested may be very helpful to me.., but you mentioned 'quick and dirty' way.  I can't get precise meaning you say.. Do you mean it's not recommended way?
  1.  about which I means it is just a test script I wrote, not many people tested or verified and the codes are very straight forward and could only be used to resample a mask.

From the log it seemed that error occured when reading a NIFTI mask.

2. You may use REST's Slice Viewer to check the EPI image data and see if it could read the image as an overlay correctly.

1. http://restfmri.net/forum/node/88
    You need to resample your mask or ROI file to the dimension of your functional images.
          There were three ways: (1) use SONG Xiao-Wei's program, from http://restfmri.net/forum/node/78                                                                               
                                                 (2) use my program, from http://restfmri.net/forum/node/81   (the third one)
                                                 (3) use SPM5:
                                                                       a. Click "Coregister"
                                                                       b. Click "New "Coreg: Reslice""
                                                                       c. "Image Defining Space": choose one of your functional image. e.g. your normalized functional image or image after Detrend and Filter.
                                                                       d. "Images to Reslice": choose the mask file or ROI definition file. e.g. BrainMask_05_61x73x61.img
                                                                       e. "Reslice Options" -> "Interpolation": choose "Nearest neighbour"
                                                                       f. Just click "Run". Then you will get the resmapled mask or ROI file with a surfix of "r".

2. I agree with Xiao-Wei, there may be something wrong with your functional images. Please check it, or you can post one pair of images here.

Dear, REST exparts
1.
 I've resampled BrainMadk_05_79x95x69 using by SPM5 Coregister function. But when I display my EPI data and resampled BrainMask with SPM5 Check Reg function, two images are apparently different position although dimensions and origins of them are completely matched.
 
 If I use Diplay and reorient function to BrainMadk_05_79x95x69, I can change only origin. In Check Reg display, my EPI image and only reoriented BrainMask are completely matched. Functional connectivity analisis have completely runned, if I use this only reoriented BrainMask.

 I wonder whether this solution is appropriate or not.

2.
 When I diplay my EPI data with SPM5 display function, 
  Dimensions:79x95x69, Origin:40x48x35

But when I see same image with REST slice viewer, following error messege always appear
   'Illegal Origin (0,0,0) I presume the origin is (40,48,35)'

What is the difference between these things?

 Thanks in advance.
Sincerely,
 Yuki

Hi!
      I think there was something wrong with your EPI images. How did you normailize your EPI images? Please attach some of your EPI images here. We may help you to check it.
                                                                   Chao-Gan

Dear, Chao-Gan
 Thank you very much. I can't attach my EPI data itself becouse of privacy probrem. But my EPI's acquisition data is following.

Coil selection = "SENSE-Head-6";
element selection = "SENSE";
connection = "d";
Dual coil = "no";
CLEAR = "yes";
FOV RL (mm) = 192;
AP (mm) = 192;
FH (mm) = 126;
Voxel size RL (mm) = 3;
AP (mm) = 3;
Slice thickness (mm) = 3.5999999;
Recon voxel size (mm) = 2.4000001;
RFOV (%) = 100;
Fold-over suppression = "no";
Matrix scan = 64;
reconstruction = 80;
Scan percentage (%) = 100;
SENSE = "yes";
P reduction (AP) = 2;
P os factor = 1;
k-t BLAST = "no";
Stacks = 1;
type = "parallel";
slices = 35;
slice thickness (mm) = 3.5999999;
slice gap = "user defined";
gap (mm) = 0;
slice orientation = "transverse";
fold-over direction = "AP";
fat shift direction = "P";
Stack Offc. AP (P=+mm) = -5.78961515;
RL (L=+mm) = 1.69300222;
FH (H=+mm) = 22.8659096;
Ang. AP (deg) = 0;
RL (deg) = -3.52474928;
FH (deg) = 0;
Minimum number of packages = 1;
Slice scan order = "ascend";
Large table movement = "no";
PlanAlign = "no";
REST slabs = 0;
Interactive positioning = "no";
Patient position = "head first";
orientation = "supine";
Scan type = "Imaging";
Scan mode = "MS";
technique = "FFE";
Contrast enhancement = "no";
Acquisition mode = "cartesian";
Fast Imaging mode = "EPI";
shot mode = "single-shot";
Echoes = 1;
partial echo = "no";
shifted echo = "no";
TE = "user defined";
(ms) = 40;
Flip angle (deg) = 80;
TR = "shortest";
Halfscan = "no";
Water-fat shift = "minimum";
Shim = "auto";
Fat suppression = "SPIR";
strength = "strong";
frequency offset = "default";
Water suppression = "no";
MTC = "no";
Research prepulse = "no";
Diffusion mode = "no";
SAR mode = "high";
B1 mode = "default";
PNS mode = "low";
Gradient mode = "default";
SofTone mode = "no";
Cardiac synchronization = "no";
Respiratory compensation = "no";
Navigator respiratory comp = "no";
Flow compensation = "no";
Temporal slice spacing = "default";
fMRI echo stabilisation = "no";
NSA = 1;
Angio = "no";
Quantitative flow = "no";
Manual start = "yes";
Dynamic study = "individual";
dyn scans = 200;
dyn scan times = "shortest";
dummy scans = 2;
immediate subtraction = "no";
fast next scan = "no";
synch. ext. device = "no";
prospect. motion corr. = "yes";
Keyhole = "no";
Preparation phases = "auto";
Manual Offset Freq. = "no";
SENSE ref. scan = "no";
MIP/MPR = "no";
Images = " M", (3) " no";
Autoview image = " M";
Calculated images = (4) " no";
Reference tissue = "White matter";
Preset window contrast = "soft";
Reconstruction mode = "real time";
reuse memory = "no";
Save raw data = "no";
Hardcopy protocol = "no";
Ringing filtering = "no";
Geometry correction = "default";
IF_info_seperator = 1634755923;
Total scan duration = "08:14.3";
Rel. signal level (%) = 100;
Act. TR/TE (ms) = "2411 / 40";
Dyn. scan time = "00:02.4";
ACQ matrix M x P = "64 x 64";
ACQ voxel MPS (mm) = "3.00 / 3.00 / 3.60";
REC voxel MPS (mm) = "2.40 / 2.40 / 3.60";
Scan percentage (%) = 100;
Packages = 1;
Min. slice gap (mm) = 0;
EPI factor = 35;
Act. WFS (pix) / BW (Hz) = "4.812 / 45.2";
BW in EPI freq. dir. (Hz) = "2396.0";
Min. WFS (pix) / Max. BW (Hz) = "4.800 / 45.3";
Min. TR/TE (ms) = "2411 / 13";
SAR / whole body = "< 6 % / 0.2 W/kg";
Whole body / level = "< 0.2 W/kg / normal";
B1 rms [uT] = 1.09401655;
PNS / level = "59 % / normal";

And then I preprocessed, (as I've mentioned before)

Preprosessing(SPM5)
 realign: register to first image
 no slice timing correction
 normalize to EPI template
   source image : one of EPI image
   source weighting image: default setting
   image to write : all of one subject's EPI data
   template: EPI nii.(in SPM5)
 smooth FWHM 6mm
Nifti>>Analyze
  using Debabeler

Thank you in advance for your help. Any suggestion will be really appreciate.
Sincerely,
 Yuki

But when I see same image with REST slice viewer, following error messege always appear
   'Illegal Origin (0,0,0) I presume the origin is (40,48,35)'

The above error means that REST could not read the origin correctly or could not recognize your EPI image format.
Maybe because REST treated your NIFTI images as ANALYZE7.5

Dear, REST experts
 I think my EPI datas are ANALYZE format. Because I change format into ANALYZE format with Debabeler. And then I check those up with MRIcro. This is also because REST have runned completely with my EPI data. I think we can't use REST if we put other kinds of data formats.

Thank you in advance for your help. Any suggestion will be really appreciate.
Sincerely,
 Yuki

Why do you convert your NIfTI images to ANALYZE images? REST V1.3 (latest release is 090422) can process NIfTI images correctly.
Two suggestions: 1. Process you data in NIfTI format after SPM's preprocess.
                             2. Modify your ANALYZE images which converted by Debabeler. Your ANALYZE images have lost their origin information, you need to modify you ANALYZE images.
                                      Chao-Gan

I convert my data into ANALYZE format because of following two reasons

 1. At first I started to analyze my data as REST English manual said. (Of corse I know it is manual of V1.1)
 2. I've tried NIFTI format before like following steps
 Preprocessing(SPM5)
 realign: register to first image
 no slice timing correction
 normalize to EPI template
   source image : one of EPI image
   source weighting image: default setting
   image to write : all of one subject's EPI data
   template: EPI nii.(in SPM5)
 smooth FWHM 6mm

⇒my preprocessed EPI data(in SPM5 Diplay): Dimensions 79x95x69, Origin 40x57x26, Voz size 2x2x2

Next I resample BrainMask_05_79x95x69 and ROI with my EPI data above(using SPM5) .
Finally, I put my preprocessed data, resampled BrainMask and resampled ROI into REST. But, following error message always appears. (This is also one of the reasons why I've used ANALYZE format  )

Welcome: kpum, 20090522_1313
REST Version: , Release:

Removing the linear trend:

Read 3D EPI functional images: "/Yuki/analyze/result_REST/result_REST_1_8/data_rest_fMRI5_HC_042".
Exception occured. ()
Error using ==> rest_ReadNiftiImage at 103
Meet error while reading the data
103#line, rest_ReadNiftiImage, in "/Applications/setup/REST/REST2007V1.3_090422/rest_ReadNiftiImage.m"
61#line, rest_readfile, in "/Applications/setup/REST/REST2007V1.3_090422/rest_readfile.m"
42#line, rest_to4d, in "/Applications/setup/REST/REST2007V1.3_090422/rest_to4d.m"
23#line, rest_detrend, in "/Applications/setup/REST/REST2007V1.3_090422/rest_detrend.m"
1170#line, Detrend, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
1278#line, btnComputeFC_Callback, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
96#line, gui_mainfcn, in "/Applications/MATLAB_R2007b/toolbox/matlab/guide/gui_mainfcn.m"
35#line, fc_gui, in "/Applications/setup/REST/REST2007V1.3_090422/fc_gui.m"
>>

Thank you in advance for your help. Any suggestion will be really appreciate.
Sincerely,
 Yuki

Thank you for your report!
      According to your report, there may be something wrong with the reading and writing under the linux system.
      I have fixed the bug of reading and writing error under the linux, unix or mac operation system. Please download it.
      Sorry for the inconvenience!
                                                 YAN Chao-Gan

Dear, YAN Chao-Gan
 New version works very well. This tool must be very helpful to my analysis. Thank you for your quick up date.
Sincerely,
 Yuki