- Verify Function Implementation: Ensure that the calculate_multi_T2 function correctly uses the df parameter. If df is not utilized or is incorrectly handled, the output will remain the same across all df values.
- Debugging Inside the Function: Add debugging statements within the calculate_multi_T2 function to confirm how the df parameter is being used.
How to make multiple graphs in one plot with a for loop?
2 次查看(过去 30 天)
显示 更早的评论
Hello, I want multiple graphs in one plot. So different df ranges plotted to see the T2. But we only get one df = -500 or df=500 all the time. What did we do wrong??
close all;
clear;
clc;
% Initial values and parameters
T2 = 0.052; % Needs to be changed > T1 needs to be checked in T2prep_function
M0 = 1;
TErange = 0.005:0.01:0.1;
T2range = 0.040:0.002:0.064; % Needs to be changed
dfrange = 100; % Scalar value for frequency range
B1var = 1; % 1 means no change in flip angle
Nspins = 500;
MLEV_size = 4; % Can be 4, 8, and 16
Mzprep = zeros(size(TErange));
compound = 'yes'; % 'yes' for compound MLEV, 'no' for simple MLEV
%% Comparing off resonances
df_range = -500:100:500; % Range of off-resonance values
% Create figure
figure(3);
hold on;
% Initialize legend cell array
legendstrings = cell(size(df_range));
% Loop over df_range and plot each line
for n = 1:length(df_range)
fprintf('Processing df = %d\n', df_range(n));
% Calculate T2fit for the current df_range value
T2fit = calculate_multi_T2(T2range, TErange, MLEV_size, Nspins, df_range(n), M0, B1var, compound);
% Print T2fit for debugging purposes
fprintf('T2fit for df = %d: %s\n', df_range(n), mat2str(T2fit));
% Plot T2fit with a DisplayName that corresponds to the current df_range value
plot(T2range, T2fit, 'DisplayName', sprintf('df = %d', df_range(n)));
% Store legend entry for current df_range value
legendstrings{n} = sprintf('df = %d', df_range(n));
end
% Finish the plot
hold off;
disp('Simulation finished');
% Add labels, legend, title, and grid
xlabel('Initial T2 (s)');
ylabel('Fitted T2 (s)');
legend(legendstrings, 'Location', 'northwest');
title('Different off-resonance values');
grid on;
0 个评论
回答(1 个)
R
2024-6-25
Hi @Marit
Your code structure appears correct for this purpose. However, to ensure that each line is plotted correctly, you should verify that the calculate_multi_T2 function returns the appropriate T2fit values for each df.
I have added debugging prints for the df values and included a dummy placeholder function, calculate_multi_T2. Refer to the modified script below:
close all;
clear;
clc;
% Initial values and parameters
T2 = 0.052; % Needs to be changed > T1 needs to be checked in T2prep_function
M0 = 1;
TErange = 0.005:0.01:0.1;
T2range = 0.040:0.002:0.064; % Needs to be changed
dfrange = 100; % Scalar value for frequency range
B1var = 1; % 1 means no change in flip angle
Nspins = 500;
MLEV_size = 4; % Can be 4, 8, and 16
Mzprep = zeros(size(TErange));
compound = 'yes'; % 'yes' for compound MLEV, 'no' for simple MLEV
% Range of off-resonance values
df_range = -500:100:500;
% Create figure
figure(3);
hold on;
% Initialize legend cell array
legendstrings = cell(size(df_range));
% Loop over df_range and plot each line
for n = 1:length(df_range)
fprintf('Processing df = %d\n', df_range(n));
% Calculate T2fit for the current df_range value
T2fit = calculate_multi_T2(T2range, TErange, MLEV_size, Nspins, df_range(n), M0, B1var, compound);
% Print T2fit for debugging purposes
fprintf('T2fit for df = %d: %s\n', df_range(n), mat2str(T2fit));
% Ensure T2fit has the correct length
if length(T2fit) ~= length(T2range)
error('T2fit length does not match T2range length for df = %d', df_range(n));
end
% Plot T2fit with a DisplayName that corresponds to the current df_range value
plot(T2range, T2fit, 'DisplayName', sprintf('df = %d', df_range(n)));
% Store legend entry for current df_range value
legendstrings{n} = sprintf('df = %d', df_range(n));
end
% Finish the plot
hold off;
disp('Simulation finished');
% Add labels, legend, title, and grid
xlabel('Initial T2 (s)');
ylabel('Fitted T2 (s)');
legend(legendstrings, 'Location', 'northwest');
title('Different off-resonance values');
grid on;
% Dummy function for demonstration purposes
function T2fit = calculate_multi_T2(T2range, ~, ~, ~, df, ~, ~, ~)
% This is a placeholder function. Replace with your actual function.
% For demonstration, it returns a simple linear relation.
T2fit = T2range * (1 + 0.1 * df / max(abs(df)))*(randi([1000, 2000])/1000);
end
When I initially tested with a dummy function, I observed two plots. To generate distinct T2fit values, I had to multiply the expression by (randi([1000, 2000])/1000). This suggests that the T2fit values might be identical for all df values because the calculate_multi_T2 function may not be correctly incorporating the df parameter in its calculations.
For your custom implementation, I recommend the following steps:
5 个评论
Image Analyst
2024-6-25
If you have any more questions, then attach your data with the paperclip icon after you read this:
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!