using for loop for multiple graph drawing

1 次查看(过去 30 天)
hi, I want to draw 10 graphs
there are x data as age, which includes 91 rows, 1 columns, and related y data (have several columns name) composed of 91 by 99 columns.
I want to draw multiple grids with for loop so that it can automatically figure out the y value which have lower than 0.05 p-value.
however, i felt difficult to do so, so I just figured out which columns have significant p-value, and just put the name of the column to draw the figure.
here is my code without for loop for 10th figure.
instaed of putting each columns name such as 'STAI_T_2', how can I put out inputs for grid?
(to run the func_regress, you would need the addpath, so please teach me the basic logic to do so)
thank you in advance!
f10 = figure;
[r, p, fig_scatter, fig_line] = func_regress(age, STAI_T_2);
if p < 0.05
fig_line.Color = [1 0.5 0.5];
end
xticks(11:2:17); xlim([10 18]);
xlabel('age', 'FontSize', 14); ylabel('STAI_T_2', 'FontSize', 14);
title(sprintf('r = %.2f, p = %.3f', r, p), 'FontSize', 12);

回答(1 个)

Ayush
Ayush 2024-1-4
To automate the process of drawing multiple graphs based on a p-value threshold, you can modify your code to loop through the columns of your data, perform the regression, and then conditionally format and display the graphs that meet the p-value criterion.
% Assuming 'data' is a matrix with 91 rows and 99 columns of y-values
% and 'age' is a column vector with 91 elements
% and 'significant_columns' is a cell array with the names of the columns that have p < 0.05
significant_columns = {'STAI_T_1', 'STAI_T_2', ...}; % Add all significant column names here
% Determine the number of rows and columns for the subplot grid
num_graphs = length(significant_columns);
num_rows = floor(sqrt(num_graphs));
num_cols = ceil(num_graphs / num_rows);
% Create a figure to hold all subplots
f = figure;
% Loop through the significant columns and create subplots
for i = 1:num_graphs
subplot(num_rows, num_cols, i);
% Get the y-data for the current column
y_data = data(:, i); % Replace 'data(:, i)' with the actual way to index your y-data
% Perform the regression
[r, p, fig_scatter, fig_line] = func_regress(age, y_data);
% Check the p-value and adjust the line color if significant
if p < 0.05
fig_line.Color = [1 0.5 0.5];
end
% Set the x-ticks, x-limits, labels, and title
xticks(11:2:17); xlim([10 18]);
xlabel('age', 'FontSize', 14); ylabel(significant_columns{i}, 'FontSize', 14);
title(sprintf('r = %.2f, p = %.3f', r, p), 'FontSize', 12);
end
Assumptions:
  • data(:, i) as a placeholder for how you might retrieve the y-data for each column. You'll need to replace this with the actual command that retrieves the y-values for each significant column in your dataset.
  • significant_columns is a cell array containing the names of the columns that you've determined to have a p-value lower than 0.05. You would need to fill this array with the actual column names that meet your significance criteria.
Thanks,
Ayush

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by