How to condense this code for plotting cdfs? How to use a for loop to plot lines.

3 次查看(过去 30 天)
Hello, I am new to MatLab and have a lot of code that I would like to condese/make easier to plot. The following code makes one subplot (I am going to eventually make 20 plots consisting of 4 subplots each). My question is, how can I condense the part of the code where I make the actual plot? I am wondering if I can use a forloop.
Also, my instructor defined the line and color for each plot ("plot_colors","plot_lines"), but I'm not sure how to use it in the code. I just set colors and line styles again at the end.
I may need to repost the question with more data so you can see the complete plot, I don't know if I made it more confusing or less.
clear all;
close all;
dataout = load('randomdata.txt');
%group data into 5 equal groups
n_groups=5;
[n_rows, n_cols] = size(dataout);
end_group=round((1:n_groups)*(n_rows)/n_groups);
start_group = [1, end_group+1];
start_group = start_group(1:n_groups);
plot_colors = ["k"; "r";"g";"r";"g"]; %Not sure what is the point of these next four lines?
plot_lines = ["-"; "-";"-";"--";"--"];
parnames = {'Column 1','Column 2','Column 3'}%... %String array of parameter names...There are 20 total
errornames = {'Column 21' 'Column 22' 'Column 23' 'Column 24'}; %String array of errors names, 4 total
%select the data for the first subplot
par_column = 1; %for now just column 1, there are 20 total
error_column = 2; %this .txt file doesn't have all the data so this really would be column 21. There are 4 total.
group = 1; %could I use this in a for loop later somehow?
[~,idx] = sort(dataout(:,error_column), 'descend');
dataout_sort = dataout(idx,:);
%From here on, would you condense this code any further?
%Make one subplot
tiledlayout(2,2)
nexttile
fig_handle = cdfplot(dataout_sort(start_group(group):end_group(group),par_column));
set(fig_handle, 'LineStyle', '-', 'Color', 'k');
%What is the point of fig_handle? Is it because it is a subplot or to set the style
%and color?
hold on
group2_handle = cdfplot(dataout_sort(start_group(2):end_group(2),par_column));
group3_handle = cdfplot(dataout_sort(start_group(3):end_group(3),par_column));
group4_handle = cdfplot(dataout_sort(start_group(4):end_group(4),par_column));
group5_handle = cdfplot(dataout_sort(start_group(5):end_group(5),par_column));
set(group2_handle, 'LineStyle', '-', 'Color', 'r');
set(group3_handle, 'LineStyle', '-', 'Color', 'g');
set(group4_handle, 'LineStyle', '--', 'Color', 'r');
set(group5_handle, 'LineStyle', '--', 'Color', 'g');
%end

采纳的回答

Cameron
Cameron 2023-2-28
tiledlayout(2,2)
nexttile
hold on
for xx = 1:n_groups
h = cdfplot(dataout_sort(start_group(xx):end_group(xx),par_column));
h.LineStyle = plot_lines(xx);
h.Color = plot_colors(xx);
end
hold off

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by