Trouble writing and saving .nii files
19 个评论
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.
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.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)