How to set axis number scaling using user input
4 次查看(过去 30 天)
显示 更早的评论
I have written a script that reads in data and creats a three diminsional plot. The current numbering on the axises is based on the the total range divided by 10 to gives segmentants based on that range. Instead of the segments being based on the total range being divided by 10 I would like the user to be able to choose how much to increment the axises by, even numbers such as 10, 50 or 100. Any help would be much appriciated.
I have included the code along with the three graph outputs. Please lmk if you have any questions or need anything else.
Chris
%clc
%clear all
%close all
%PT7 35968 240000 BSG 58448
dyno_cycles = 1660.4; %num of hours
veh_speed = 72.27; %ave speed (km per h)
CEM = 240000; %cust equivelent miles (km)
T_max = 450;
T_min = 0;
T_steps = 10
RPM_max = 6000;
RPM_min = 0;
RPM_steps = 10;
%Open List Files
%[filename1,pathname1] = uigetfile({'*.xlsx'},'Select Schedule');
%if filename1 == 0
% return
%else
% filename{1} = filename1;
% pathname{1} = pathname1;
%end
%Import data from an excel file.
%fullpathname{1} = strcat (pathname{1}, filename{1});
%clc
%dataDyno = xlsread(fullpathname{1},1);
%total_test_time(1) = dataDyno(length(dataDyno(:,1)),1);
%time_hours = round(total_test_time(1)/(3600)*100)/100;
[filename2,pathname2] = uigetfile({'*.xlsx'},'10x10 File Selector');
if filename2 ~= 0
filename{2} = filename2;
pathname{2} = pathname2;
end
fullpathname{2} = strcat (pathname{2}, filename{2});
dataVeh_10 = flipud(xlsread(fullpathname{2},1));
time_10_x_10 = CEM/veh_speed;
total_test_time_10(2) = time_10_x_10*3600;
time_hours_veh = round(time_10_x_10);
dataveh_time_frac = dataVeh_10;
[filenameCAE, pathnameCAE] = uigetfile({'*.xlsx'},'CAE Data File Selector');
fullpathname{2} = strcat (pathnameCAE, filenameCAE);
caeData = xlsread(fullpathname{2},1);
[filenameSN, pathnameSN] = uigetfile({'*.xlsx'},'Material Data File Selector');
fullpathname{3} = strcat (pathnameSN, filenameSN);
materialData = xlsread(fullpathname{3},1);
%%Data Storing/Sorting
%try
%Determine the test schedules names
for a = 2
idx(a) = max(strfind(char(filename{a}),'.'));
name = char(filename{a});
fname{a} = name(1:idx(a)-1);
end
dataLength(1) = length(dataDyno);
% dataLength(2) = length(dataVeh);
%Store schedule data into single data file
data(1:max(dataLength),1:3,1:2) = 0;
data(1:dataLength(1),1:3,1) = dataDyno;
%data(1:dataLength(2),1:3,2) = dataVeh;
%Determine speed and torque extrema from reference cycle
%maxSpeedRef = max(max(data(:,2,:)));
%maxTorqueRef = max(max(data(:,3,:)));
%minSpeedRef = min(min(data(:,2,:)));
%minTorqueRef = min(min(data(:,3,:)));
%minSpeedRef(minSpeedRef<0) = 0;
%minTorqueRef(minTorqueRef<0) = 0;
%Determine speed and torque extrema from reference cycle
maxSpeedRef = RPM_max;
maxTorqueRef = T_max;
minSpeedRef = RPM_min;
minTorqueRef = T_min;
minSpeedRef(RPM_min<0) = 0;
minTorqueRef(T_min<0) = 0;
%Determine range of speed and torque from reference cycle
speed_range = maxSpeedRef - minSpeedRef;
torque_range = maxTorqueRef - minTorqueRef;
%Number of segments for histogram plots
resolution = 11;
%Array of speed and torque intervals
%Adjust axis steps here
input_array = zeros(resolution,2);
for x=1:1:resolution
%input_array(x,1) = speed_range/(resolution-1)*(x-1) + minSpeedRef;
%input_array(x,2) = torque_range/(resolution-1)*(x-1) + minTorqueRef;
input_array(x,1) = speed_range/(resolution-1)*(x-1) + minSpeedRef;
input_array(x,2) = torque_range/(resolution-1)*(x-1) + minTorqueRef;
end
%Array for average of speed and torque intervals
RPM_in = zeros(resolution-1,1);
Torque_in = zeros(resolution-1,1);
for x=1:1:resolution-1
RPM_in(x,1) = input_array(x,1)+speed_range/(resolution-1)/2;
Torque_in(x,1) = input_array(x,2)+torque_range/(resolution-1)/2;
end
%Create time percent matrix using time_percV2 function
%Time percent matrix insert here......
%Read in rows and colombs from 10x10
summary = zeros(resolution-1,resolution-1,1);
for a=1
try
time_percent_matrix = time_percV2(data(1:dataLength(a),:,a),input_array,resolution);
catch
close(messageIP)
messageError = msgbox('Error in calculations. Ensure proper data files are uploaded');
return
end
%Disregard data points that are not in the reference schedules bounds
%summary(:,:,a) = time_percent_matrix;
summary(:,:,a) = dataVeh_10;
end
%summary(:,:,a) = dataVeh_10;
%%Colormap
clrMap = [.7 0 0
.78 0 0
1 .27 0
1 .74 0
1 1 .3
];
Nf = 256; % size of new color map
m = size(clrMap,1);
t0 = linspace(0,1,m)';
t = linspace(0,1,Nf)';
red = interp1(t0,clrMap(:,1),t);
green = interp1(t0,clrMap(:,2),t);
blue = interp1(t0,clrMap(:,3),t);
cmap = [red,green,blue];
cmap(1,:) = 1;
cmap(end,:) = 1;
jetColorMap = jet(256);
jetColorMap(1,:) = 1;
autumn1 = autumn(256);
autumn1(end,:) = 1;
autumn2 = autumn(256);
autumn2(1,:) = 1;
%%Figure 1 - Time fraction histogram
fig(1) = figure;
%x and y indices for histograms
X = [speed_range/(2*(resolution-1))+minSpeedRef maxSpeedRef-speed_range/(2*(resolution-1))];
Y = [torque_range/(2*(resolution-1))+minTorqueRef maxTorqueRef-torque_range/(2*(resolution-1))];
Z = zeros(resolution-1,resolution-1,2);
for a=1
%time percent matrix
Z(:,:,a) = flipud(summary(:,:,a))
if a==2
Z(:,:,a) = dataveh_time_frac;
else
end
%figure
plot(1,a);
Z((Z(:,:,:)==0))=nan;
Z(:,:,a)
imagesc(X,fliplr(Y),Z(:,:,a));
colorbar;
colormap(flipud(autumn1));
%colormap(jetColorMap);
maxInd_Summary = max(max(max(summary(:,:,:))));
%caxis([-1 maxInd_Summary]);
caxis([-1 10]);
grid on
%Set plot titles/labels
set(gca,'XLim',[minSpeedRef maxSpeedRef]);
set(gca,'XTick',round((minSpeedRef:speed_range/(resolution-1):maxSpeedRef)));
set(gca,'YLim',[minTorqueRef maxTorqueRef]);
set(gca,'YTick',round((minTorqueRef:torque_range/(resolution-1):maxTorqueRef)));
set(gca,'YDir','normal');
xlabel('Engine Speed (RPM)','fontsize',14);
ylabel('Engine Torque (Nm)','fontsize',14);
title(strcat(fname{a},' Time Fraction (%)'),'fontsize',14)
set(gcf,'units','inches','position',[9 .5 7 5.5])
data_veh_labels = flipud(dataveh_time_frac)
%Set labels for each load case
for i = 1:1:resolution-1
if a==2
for j = 1:1:resolution-1
tfText{j,i} = num2str(round(data_veh_labels(j,i,1)*10)/10);
t = text((input_array(2,1)-input_array(1,1))*(i-1)+(input_array(2,1)-input_array(1,1))/2+input_array(1,1),(input_array(2,2)-input_array(1,2))*(j-1)+(input_array(2,2)-input_array(1,2))/2+input_array(1,2),tfText(j,i));
set(t,'horizontalAlignment','center','fontsize',12);
end
else
for j = 1:1:resolution-1
tfText{j,i} = num2str(round(summary(j,i,a)*10)/10);
if summary(j,i,a)>0
t = text((input_array(2,1)-input_array(1,1))*(i-1)+(input_array(2,1)-input_array(1,1))/2+input_array(1,1),(input_array(2,2)-input_array(1,2))*(j-1)+(input_array(2,2)-input_array(1,2))/2+input_array(1,2),tfText(j,i));
set(t,'horizontalAlignment','center','fontsize',12);
end
end
end
end
end
%%Figure 4 - Alternating Stress Map
%Acquire CAE data from user input
caeTorque = caeData(:,1);
caeSpeed = caeData(:,2);
caeFSF = caeData(:,3);
caeSA = caeData(:,4);
caeSM = caeData(:,5);
caeTemp = caeData(:,6);
%Acquire material data from user input
snTemp = materialData(:,1);
snSlope = materialData(:,2);
enduranceLim = materialData(:,3);
cycleLim = materialData(:,4);
xnodes = linspace(minSpeedRef, maxSpeedRef, resolution-1);
ynodes = linspace(minTorqueRef, maxTorqueRef, resolution-1);
%Initialize material data variables
caeSNslope = zeros(length(caeTemp),1);
caeEnduranceLim = zeros(length(caeTemp),1);
caeCycleLim = zeros(length(caeTemp),1);
b = zeros(length(caeTemp),1);
Sf_temp = zeros(length(caeTemp),1);
Sf_p = zeros(length(caeTemp),1);
SAR = zeros(length(caeTemp),1);
%Interpolation method of SN data - includes interpolation for SN slope, endurance limit and cycle limit
if length(snTemp)>1
for snDP=1:1:length(snTemp)-1
rows1 = find(caeTemp(:)>=snTemp(snDP) & caeTemp(:)<=snTemp(snDP+1));
for a=1:1:length(rows1)
caeSNslope(rows1(a)) = (caeTemp(rows1(a))-snTemp(snDP))/(snTemp(snDP+1)-snTemp(snDP))*(snSlope(snDP+1)-snSlope(snDP))+snSlope(snDP);
caeEnduranceLim(rows1(a)) = (caeTemp(rows1(a))-snTemp(snDP))/(snTemp(snDP+1)-snTemp(snDP))*(enduranceLim(snDP+1)-enduranceLim(snDP))+enduranceLim(snDP);
caeCycleLim(rows1(a)) = (caeTemp(rows1(a))-snTemp(snDP))/(snTemp(snDP+1)-snTemp(snDP))*(cycleLim(snDP+1)-cycleLim(snDP))+cycleLim(snDP);
end
end
rows2 = find(caeSNslope == 0);
for a=1:1:length(rows2)
if caeTemp(rows2(a))<snTemp(1)
caeSNslope(rows2(a)) = (caeTemp(rows2(a))-snTemp(1))/(snTemp(2)-snTemp(1))*(snSlope(2)-snSlope(1))+snSlope(1);
caeEnduranceLim(rows2(a)) = (caeTemp(rows2(a))-snTemp(1))/(snTemp(2)-snTemp(1))*(enduranceLim(2)-enduranceLim(1))+enduranceLim(1);
caeCycleLim(rows2(a)) = (caeTemp(rows2(a))-snTemp(1))/(snTemp(2)-snTemp(1))*(cycleLim(2)-cycleLim(1))+cycleLim(1);
elseif caeTemp(rows2(a))>snTemp(end)
caeSNslope(rows2(a)) = (caeTemp(rows2(a))-snTemp(end-1))/(snTemp(end)-snTemp(end-1))*(snSlope(end)-snSlope(end-1))+snSlope(end-1);
caeEnduranceLim(rows2(a)) = (caeTemp(rows2(a))-snTemp(end-1))/(snTemp(end)-snTemp(end-1))*(enduranceLim(end)-enduranceLim(end-1))+enduranceLim(end-1);
caeCycleLim(rows2(a)) = (caeTemp(rows2(a))-snTemp(end-1))/(snTemp(end)-snTemp(end-1))*(cycleLim(end)-cycleLim(end-1))+cycleLim(end-1);
end
end
%SN curve calculations (Sf')
for caeDP=1:1:length(caeTemp)
b(caeDP) = -1/caeSNslope(caeDP);
Sf_temp(caeDP) = (-1/caeSNslope(caeDP))*(log10(caeCycleLim(caeDP))- log10(1))-(log10(caeEnduranceLim(caeDP)));
Sf_p(caeDP) = 10.^(Sf_temp(caeDP)*-1);
SAR(caeDP) = caeSA(caeDP)/(1-caeSM(caeDP)/Sf_p(caeDP));
end
else
%SN curve calculations (Sf')
for caeDP=1:1:length(caeTemp)
b(caeDP) = -1/snSlope;
Sf_temp(caeDP) = (-1/snSlope)*(log10(cycleLim)- log10(1))-(log10(enduranceLim));
Sf_p(caeDP) = 10.^(Sf_temp(caeDP)*-1);
SAR(caeDP) = caeSA(caeDP)/(1-caeSM(caeDP)/Sf_p(caeDP));
end
end
%gridfit function used to formulate stress map, b map and Sf' map
[stress_map, speed_map, torque_map] = gridfit (caeSpeed,caeTorque,SAR,xnodes,ynodes);
b_map= gridfit (caeSpeed,caeTorque,b,xnodes,ynodes);
Sf_p_map = gridfit (caeSpeed,caeTorque,Sf_p,xnodes,ynodes);
%%Figure 6 - Stress History Per Load Case Plot
fig(6) = figure;
stress = zeros(resolution-1,resolution-1,2);
%Z2 calculation (stress)
for a=1
%only make stresses which are seen from tests visible
Z2=flipud(stress_map');
Z2(isnan(Z(:,:,a)))=nan;
Z2(Z2<0)=nan;
%Plot stress
plot(1,a);
imagesc(X,fliplr(Y),Z2);
set(gca,'YDir','normal');
maxStress = 120; %max(max(stress_map))
colorbar;
colormap(jetColorMap);
caxis([-2, maxStress]) ;
grid on;
%Set plot titles/labels
set(gca,'XLim',[minSpeedRef maxSpeedRef]);
set(gca,'XTick',round((minSpeedRef:speed_range/(resolution-1):maxSpeedRef)));
set(gca,'YLim',[minTorqueRef maxTorqueRef]);
set(gca,'YTick',round((minTorqueRef:torque_range/(resolution-1):maxTorqueRef)));
xlabel('Engine Speed (RPM)');
ylabel('Engine Torque (Nm)');
title(strcat(fname{a},' Stress History (MPa)'), 'fontsize',14);
set(gcf,'units','inches','position',[4.5 .5 8 5.5])
%Set labels for each load case
Z2_text = flipud(Z2);
for e = 1:1:resolution-1
for d = 1:1:resolution-1
saText{d,e} = num2str(round(Z2_text(d,e)*10)/10);
if Z2_text(d,e) >= 0
t = text((input_array(2,1)-input_array(1,1))*(e-1)+(input_array(2,1)-input_array(1,1))/2+input_array(1,1),(input_array(2,2)-input_array(1,2))*(d-1)+(input_array(2,2)-input_array(1,2))/2+input_array(1,2),saText(d,e));
set(t,'horizontalAlignment','center', 'fontsize',12);
end
end
end
stress(:,:,a) = flipud(Z2);
end
maxStress = 120
%%Damage Calculations
%Nf - cycles to failure calculated from stress at load case and slope of SN curve
Nf = zeros(resolution-1,resolution-1,2);
%n - cycles at a load cases
n = zeros(resolution-1,resolution-1,2);
dam_tot_col_by_speed = zeros(resolution-1,resolution-1,2);
dataVeh_10_1 = flipud(dataVeh_10);
for a=1
if a==2
for i=1:1:resolution-1
for j=1:1:resolution-1
Nf(j,i,a) = ((stress(j,i,a))/Sf_p_map(j,i))^(1/b_map(j,i));
n(j,i,a) = dataVeh_10_1(j,i,1)/100*total_test_time_10(2)*RPM_in(i)/120;
%dam_tot_col_by_speed(j,i,a) = n(j,i,a)/Nf(j,i,a)/total_test_time_10(5);
dam_tot_col_by_speed(j,i,a) = n(j,i,a)/Nf(j,i,a);
end
end
dam_tot_col_by_speed(isinf(dam_tot_col_by_speed)) = 0;
dam_tot_col_by_speed(isnan(dam_tot_col_by_speed)) = 0;
damage_total(a) = sum(sum(dam_tot_col_by_speed(:,:,a)));
else
for i=1:1:resolution-1
for j=1:1:resolution-1
Nf(j,i,a) = ((stress(j,i,a))/Sf_p_map(j,i))^(1/b_map(j,i));
n(j,i,a) = summary(j,i,a)/100*total_test_time(a)*RPM_in(i)/120;
dam_tot_col_by_speed(j,i,a) = n(j,i,a)/Nf(j,i,a)*dyno_cycles;
end
end
dam_tot_col_by_speed(isinf(dam_tot_col_by_speed)) = 0;
dam_tot_col_by_speed(isnan(dam_tot_col_by_speed)) = 0;
damage_total(a) = sum(sum(dam_tot_col_by_speed(:,:,a)));
end
end
%%Figure 9 - Damage per Load Case Plot
fig(9) = figure;
for a=1
%Plot damage percentage
if a==2
plot(1,a)
bar3c(flipud(dam_tot_col_by_speed(:,:,a)))
colorbar;
colormap(jet);
caxis([0 0.1]);
%Set plot titles/labels
set(gca,'XTick',[1:1:11]);
set(gca,'XTickLabels',round((minSpeedRef:speed_range/(resolution-1):maxSpeedRef)));
set(gca,'YTick',[1:1:11]);
set(gca,'YTickLabels',round(flipud((minTorqueRef:torque_range/(resolution-1):maxTorqueRef))));
xlabel('&&&engine speed (RPM)');
ylabel('&&&engine torque (Nm)');
title(strcat(fname{a},' Damage per Load Case'),'fontweight','bold');
else
plot(1,a)
bar3c((dam_tot_col_by_speed(:,:,a)))
colorbar;
colormap(jet);
caxis([0 0.1]);
set(gca,'XTick',[1:1:11]);
set(gca,'XTickLabels',round((minSpeedRef:speed_range/(resolution-1):maxSpeedRef)));
xtickangle(-45)
set(gca,'XTickLabelRotation',-45)
set(gca,'YTick',[1:1:11]);
ax = gca ;
ax.YDir = 'normal';
set(gca,'YTickLabels',round(flipud((minTorqueRef:torque_range/(resolution-1):maxTorqueRef))));
ytickangle(45)
xlabel('Engine Speed (RPM)');
ylabel('Engine Torque (Nm)');
title(strcat(fname{a},' Damage per Load Case'),'fontsize',14);
set(gcf,'units','inches','position',[0 .5 7 5.5])
set(gca,'FontSize',12)
end
end
%%Figure 10 - Damage Percentage per Load Case Plot
for a=1
dam_tot_col_by_speed(isinf(dam_tot_col_by_speed)) = 0;
dam_tot_col_by_speed(isnan(dam_tot_col_by_speed)) = 0;
%Plot damage percentage
damage_total(a) = sum(sum(dam_tot_col_by_speed(:,:,a)));
dam_perc(:,:,a) = dam_tot_col_by_speed(:,:,a)/damage_total(a)*100;
dam_perc(:,:,a) = flipud(dam_perc(:,:,a));
%Total damage
window_1= msgbox(sprintf('The total damage is %d',(damage_total(1))));
fprintf('%d\n',(damage_total(1)));
B=damage_total(1);
save('Tot_Damage.txt','B','-ascii')
type('Tot_Damage.txt')
end
0 个评论
回答(1 个)
Aditya
2025-6-12
Hi Chris,
You can modify the plot statically by taking the input from the user once and generating the plot accordingly, or you can modify it dynamically by placing a tool (such as a slider) within the plot to control the increment value interactively. Here’s how you can achieve it:
1) Using the input() function (Static Approach)
This method is useful when you want the user to specify an increment before generating the plot.
valid_values = [10, 50, 100];
increment = input('Enter axis increment (10, 50, or 100): ');
while ~ismember(increment, valid_values)
increment = input('Invalid input! Please enter 10, 50, or 100: ');
end
figure;
x = linspace(0, 100, 50);
y = linspace(0, 100, 50);
z = sin(x) + cos(y);
plot3(x, y, z, 'o');
grid on;
xticks(0:increment:max(x));
yticks(0:increment:max(y));
zticks(min(z):increment:max(z));
2) Using a Slider for Dynamic Updates
This approach is useful if you want to allow users to adjust the increment interactively while updating the plot in real-time.
function dynamic_axis_scaling
fig = figure;
x = linspace(0, 100, 50);
y = linspace(0, 100, 50);
z = sin(x) + cos(y);
ax = axes('Parent', fig);
plot3(ax, x, y, z, 'o');
grid on;
% Create slider for dynamic updates
slider = uicontrol('Style', 'slider', 'Min', 10, 'Max', 100, ...
'Value', 10, 'SliderStep', [5/90, 5/90], ...
'Position', [20, 20, 300, 20], ...
'Callback', @(src, event) update_ticks(ax, round(src.Value)));
label = uicontrol('Style', 'text', 'Position', [330, 20, 50, 20], ...
'String', '10');
function update_ticks(ax, increment)
xticks(ax, 0:increment:max(x));
yticks(ax, 0:increment:max(y));
zticks(ax, min(z):increment:max(z));
label.String = num2str(increment);
end
end
You can refer to the above two examples to modify your code to achieve the similar behaviour.
I hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!