You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Trouble writing and saving .nii files
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1749619/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1749624/image.png)
19 Comments
Hi @Grace,
In order to resolve this issue, I will suggest a revised approach:
When extracting each flip angle’s data for saving, directly access it without reshaping unnecessarily and modify your saving loop, here is modified code snippet,
for i = 1:size(T1, 4)
% Directly convert the T1 data for this angle
Data = double(T1(:, :, :, i));
% Define voxel size
VoxSize = [0.6, 0.6, 1];
% Create a name for the NIfTI file
NameForSaving = sprintf('T1_Map_Angle%d.nii', i);
% Define the file location for saving
filelocation = 'location_hidden';
fullpath = fullfile(filelocation, NameForSaving);
% Save the data as a NIfTI file using Nii_Saver
Nii_Saver(Data, VoxSize, fullpath);
end
Make sure that `Nii_Saver` correctly interprets the dimensions of `Data`. If it expects a specific orientation or arrangement of dimensions (e.g., [X,Y,Z]), make sure that you are providing it in that format.After saving each file, it might be beneficial to load them back into MATLAB using a tool like `niftiread` to verify their integrity and correct dimensionality before moving on to visualization in FSLeyes. Hope this helps.
Hi @Grace,
Here’s an adjusted code snippet that keeps the original structure while saving:
for i = 1:size(T1, 4)
% Directly convert the T1 data for this angle
Data = double(T1(:, :, :, i)); % This extracts the correct angle
% Define voxel size
VoxSize = [0.6, 0.6, 1];
% Create a name for the NIfTI file
NameForSaving = sprintf('T1_Map_Angle%d.nii', i);
% Define the file location for saving
filelocation = 'location_hidden';
fullpath = fullfile(filelocation, NameForSaving);
% Save the data as a NIfTI file using Nii_Saver
Nii_Saver(Data, VoxSize, fullpath);
end
If reordering is necessary (to get slices organized as 1,2,...170 for each angle), consider implementing a more explicit reordering function before saving:
for i = 1:size(T1, 4)
Data = double(T1(:, :, :, i));
% Assuming you want to rearrange slices
reorderedData = reshape(permute(Data, [1, 2, 4, 3]), [512, 512, 170]);
VoxSize = [0.6, 0.6, 1];
NameForSaving = sprintf('T1_Map_Angle%d.nii', i);
fullpath = fullfile(filelocation, NameForSaving);
Nii_Saver(reorderedData, VoxSize, fullpath);
end
Before running on your complete dataset, consider testing with smaller sample arrays to confirm that your process works as expected without introducing errors into your T1 values. I will wait for @Cris LaPierre suggestion.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751634/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751639/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751644/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751649/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751654/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751659/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751664/image.png)
Hi @Grace,
Based on your description, if I comprehend correctly, it seems like you are trying to rearrange your data in a specific order after performing calculations. You want to display the slices in the order of 1, 2, 3, 4, 5...170 instead of the current order of 1, 6, 11, 16...2, 7, 12, 17...3, 8, 13, 18...4, 9, 14, 19...5, 10, 15, 20...170. To achieve this task, you can use indexing and reshaping operations. Here's an example code that demonstrates how you can rearrange the data:
% Assuming your data is stored in a variable called 'T1'
% Get the size of your data
[height, width, slices_per_angle, num_angles] = size(T1);
% Reshape the data to [height x width x (slices_per_angle *
num_angles)]
reshaped_data = reshape(T1, [height, width, slices_per_angle *
num_angles]);
% Rearrange the slices to the desired order
new_order = reshape(1:slices_per_angle*num_angles,
[num_angles,slices_per_angle]);
new_order = new_order'; % Transpose to match desired order
% Index and select slices according to the new order
rearranged_data = reshaped_data(:, :, new_order(:), :);
So, as you can see, I reshaped the original data `T1` into a new shape where all the slices are concatenated together, then created a matrix `new_order` that represents the desired order of the slices. By indexing with `new_order(:)`, you can select and rearrange the slices accordingly.This should result in `rearranged_data` having the desired order of slices: 1, 2, 3, 4, 5...170. I assume your data is stored in a four-dimensional array with dimensions `[height x width x slices_per_angle x num_angles]` because adjustments may be needed if your data is organized differently.
I will assume @Cris LaPierre probably have a different approach. For now, this should help. Please let me know if you have any further questions.
Hi @ Cris LaPierre,
To save each angle from a 4D array as individual NIfTI files, here is the code snippet.
% Assuming 'data' is your 4D array with dimensions [height, width,
slices_per_angle, num_angles]
[height, width, slices_per_angle, num_angles] = size(data);
for angle = 1:num_angles
% Extract the data for the current angle
angle_data = data(:, :, :, angle);
% Define the filename for the NIfTI file
filename = sprintf('angle_%d.nii', angle);
% Save the angle data as a NIfTI file
niftiwrite(angle_data, filename);
end
So, basically you will loop through each angle, extract the corresponding 3D slice, and save it as a NIfTI file using niftiwrite. The filenames will be then dynamically generated to reflect the angle number. Hope, you have a better solution.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)