Compact way to plot multiple listdlg items depending on user input.
7 ビュー (過去 30 日間)
古いコメントを表示
I want to find a way to write a compact script that requests user input on a listdlg box and plots a graph depending on the parameters chosen (could be multiple parameters). I am relatively new to matlab and I want to write a generalized condition instead of hard-coding all possible combinations and plotting the outcome. I want to be able to plot up to 6 parameters in one graph with a compact code instead of writing the conditions 6!=6x5x4x3x2x1=720 times :/. To get a feel of my code:
list={'Live data','Historic data'};
[type,tf] = listdlg('ListString',list);
if type==1
list_title={'a','b','c'};
[Topic_ID,tf] = listdlg('ListString',list_title);
if 1 >= Topic_ID <= 3
list_var={'NO2','CO','SO2','H2S','humidity','temperature'};
[var_pos,tf] = listdlg('PromptString','Select a variable to plot:','ListString', list_var);
var_size=size(var_pos);
if var_pos==1 %Plot only NO2
figure(1)
t=datenum(datetime,'yyyy-mm-ddTHH:MM:SS.FFF');
plot(t,NO2)
datetick('x','dd-mmm-yyyy HH:MM:SS','keepticks','keeplimits');
xlabel('Time (dd-mmm-yyyy HH:MM:SS)');
ylabel('NO2 (ppb)');
title('NO2');
legend('NO2');
end
if isequal(var_pos,[1,2]) %Plot CO and NO2
figure(7)
t=datenum(datetime,'yyyy-mm-ddTHH:MM:SS.FFF');
plot(t,NO2,t,CO)
datetick('x','dd-mmm-yyyy HH:MM:SS','keepticks','keeplimits');
xlabel('Time (dd-mmm-yyyy HH:MM:SS)');
ylabel('CO & NO2 (ppb)');
title('CO & NO2');
legend('NO2','CO');
end
Where var_pos is the matrix that contains which list items were chosen. Appreciate any help. I can't figure it out.
0 件のコメント
回答 (1 件)
Jacob Mathew
2025 年 1 月 9 日
Hey Milton,
Assuming that the graph itself is going to be the same and each plot is going to be on top of the other, you can use the hold on command to keep the same figure and plot the necessary lines. The following code a simple example of using a for loop to iterate and check for what all lines need to be plotted:
% Define the lines
x = linspace(0, 2, 100);
lines = {
x;
x.^2;
sin(x);
cos(x);
log(x + 1);
exp(x)
};
% Line labels
line_labels = {
'Line 1: y = x';
'Line 2: y = x^2';
'Line 3: y = sin(x)';
'Line 4: y = cos(x)';
'Line 5: y = log(x + 1)';
'Line 6: y = e^x'
};
% Binary array to decide which lines to plot
line_selector = [1, 0, 1, 0, 1, 0];
% Create a new figure
figure;
hold on;
% Plot the lines based on the binary array
for i = 1:length(line_selector)
if line_selector(i)
plot(x, lines{i}, 'DisplayName', line_labels{i});
end
end
% Add labels and legend
xlabel('x');
ylabel('y');
title('Selected Lines Plot');
legend show;
grid on;
% Hold off to stop adding to the current plot
hold off;
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!