How to make multiple graphs in one plot with a for loop?

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
Processing df = -500
Unrecognized function or variable 'calculate_multi_T2'.
% 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;

Answers (1)

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
Processing df = -500
T2fit for df = -500: [0.043992 0.0461916 0.0483912 0.0505908 0.0527904 0.05499 0.0571896 0.0593892 0.0615888 0.0637884 0.065988 0.0681876 0.0703872]
Processing df = -400
T2fit for df = -400: [0.046044 0.0483462 0.0506484 0.0529506 0.0552528 0.057555 0.0598572 0.0621594 0.0644616 0.0667638 0.069066 0.0713682 0.0736704]
Processing df = -300
T2fit for df = -300: [0.070776 0.0743148 0.0778536 0.0813924 0.0849312 0.08847 0.0920088 0.0955476 0.0990864 0.1026252 0.106164 0.1097028 0.1132416]
Processing df = -200
T2fit for df = -200: [0.055656 0.0584388 0.0612216 0.0640044 0.0667872 0.06957 0.0723528 0.0751356 0.0779184 0.0807012 0.083484 0.0862668 0.0890496]
Processing df = -100
T2fit for df = -100: [0.050976 0.0535248 0.0560736 0.0586224 0.0611712 0.06372 0.0662688 0.0688176 0.0713664 0.0739152 0.076464 0.0790128 0.0815616]
Processing df = 0
T2fit for df = 0: [NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]
Processing df = 100
T2fit for df = 100: [0.078276 0.0821898 0.0861036 0.0900174 0.0939312 0.097845 0.1017588 0.1056726 0.1095864 0.1135002 0.117414 0.1213278 0.1252416]
Processing df = 200
T2fit for df = 200: [0.084876 0.0891198 0.0933636 0.0976074 0.1018512 0.106095 0.1103388 0.1145826 0.1188264 0.1230702 0.127314 0.1315578 0.1358016]
Processing df = 300
T2fit for df = 300: [0.08184 0.085932 0.090024 0.094116 0.098208 0.1023 0.106392 0.110484 0.114576 0.118668 0.12276 0.126852 0.130944]
Processing df = 400
T2fit for df = 400: [0.069476 0.0729498 0.0764236 0.0798974 0.0833712 0.086845 0.0903188 0.0937926 0.0972664 0.1007402 0.104214 0.1076878 0.1111616]
Processing df = 500
T2fit for df = 500: [0.086064 0.0903672 0.0946704 0.0989736 0.1032768 0.10758 0.1118832 0.1161864 0.1204896 0.1247928 0.129096 0.1333992 0.1377024]
% Finish the plot
hold off;
disp('Simulation finished');
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:
  1. 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.
  2. Debugging Inside the Function: Add debugging statements within the calculate_multi_T2 function to confirm how the df parameter is being used.

5 Comments

Thank you very much for your help and answer! We do get a different T2fit value for every df... It just seems like it overlays in the graph...
This is our calculate_multi_T2:
function T2fit = calculate_multi_T2(T2range, TErange, MLEV_size, Nspins, dfrange, M0, B1var, comp)
compound = comp;
T2fit = zeros(size(T2range));
Mzprep = zeros(size(TErange));
for m = 1:length(T2range)
T2init = T2range(m);
for n = 1:length(TErange)
TE = TErange(n);
[times_180, phases_180] = MLEV_pulses(MLEV_size, TE, compound);
% disp(['Calculating Mzprep for T2init = ', num2str(T2init), ', TE = ', num2str(TE)]);
Mzprep(n) = T2prep_function(T2init, TE, Nspins, dfrange, times_180, phases_180, B1var, compound);
end
disp('Mz determined');
T2fit(m) = calculate_T2(M0, T2init, TErange, Mzprep);
disp('T2 determined');
end
end
Maybe you can see something that is wrong? Thank you already!
The function you have shared contains multiple dependencies on functions like calculate_T2, T2prep_function, and MLEV_pulses. Without the overall context and all involved resource files, it is very difficult to pinpoint the issue.
However, as you mentioned, you get different T2fit values for each df, suggesting they are too close to each other, making the plot look like a single line. To improve the visibility and differentiation of the lines, you can consider several strategies such as zooming in, using a log scale, or adding a small offset to each line to separate them visually.
But in our legend we only see df = 500, if everything would work we should see df = -500 till df = 500 with steps of 100 in the legend right?
@R ran the code you shared here with a dummy function and the plot in his answer shows df from -500 to 500 with sstep size of 100. So your function might not be giving you the outputs correctly
@Marit, please supply ALL your required functions so we can finally get this solved.
If you have any more questions, then attach your data with the paperclip icon after you read this:

Sign in to comment.

Categories

Asked:

on 25 Jun 2024

Commented:

on 25 Jun 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!