Why do I get the error "Invalid or deleted object" for this code
18 次查看(过去 30 天)
显示 更早的评论
I have the code below which I have run before without problems but I suddenly get the error message "Error using stackedplot (line 100)
Invalid or deleted object." at the line "stackedplot(acctable,{'accX','accY','accZ'})". Can anyone help with this?
file = ('F:\MATLAB\Vessel_data\Galloper\Seacat Liberty\improveddata2019-09-30');
load (file)
%Create table of two columns time and acceleration
if isfield(data,'COM23_heading')== 1
full_trip = table(data.timeStamp,data.accX,data.accY,data.accZ,data.roll,data.pitch,data.yaw,data.COM23_speed,data.COM23_heading,data.COM23_lat,data.COM23_lon,data.dist2harbour,data.dist2turbine,data.inField,'VariableNames',{'timeStamp','accX[G]','accY[G]','accZ[G]','roll[deg]','pitch[deg]','yaw[deg]','speed[kph]','heading[deg]','lat','lon','dist2harbour','dist2turbine','inField'});
else
full_trip = table(data.timeStamp,data.accX,data.accY,data.accZ,data.roll,data.pitch,data.yaw,data.COM7_speed,data.COM7_heading,data.COM7_lat,data.COM7_lon,data.dist2harbour,data.dist2turbine,data.inField,'VariableNames',{'timeStamp','accX[G]','accY[G]','accZ[G]','roll[deg]','pitch[deg]','yaw[deg]','speed[kph]','heading[deg]','lat','lon','dist2harbour','dist2turbine','inField'});
end
%Convert timestamp to datenum format
full_trip.date = datetime(full_trip.timeStamp,'ConvertFrom',"datenum");
%Create time/durtion in second by converting datetime to seconds
durtime = datetime(full_trip.date, 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SS', 'Format', 'yyyy-MM-dd HH:mm:ss.SS');
durtime(isnat(durtime)) = datetime(full_trip.date(isnat(durtime)), 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
full_trip.date = durtime;
full_trip.duration = seconds(full_trip.date - full_trip.date(1));
%convert acc in G to m/s^2
full_trip.accX = convacc(full_trip.("accX[G]"),'G''s','m/s^2');
full_trip.accY = convacc(full_trip.("accY[G]"),'G''s','m/s^2');
full_trip.accZ = convacc(full_trip.("accZ[G]"),'G''s','m/s^2');
%Rearrange and remove timestamp variable
full_trip = movevars(full_trip, {'date','duration','accX','accY','accZ'}, 'Before', "accX[G]");
%Create a categorial value of in windfarm
full_trip.location = categorical(full_trip.inField);
full_trip.location = renamecats(full_trip.location,"false","Transit");
full_trip.location = renamecats(full_trip.location,"true","Windfarm");
Visualize data
acctable = table(full_trip.duration,full_trip.date,full_trip.accX,full_trip.accY,full_trip.accZ,'VariableNames',{'duration','date','accX','accY','accZ'});
acctimetable = table2timetable(acctable);
windowlength = 1800;
stdaccY = retime(acctimetable,"regular",@std,"TimeStep",seconds(windowlength));
figure(1)
subplot(3,2,[1,2])
geoplot(full_trip.lat,full_trip.lon)
subplot(3,2,3)
stackedplot(acctable,{'accX','accY','accZ'})
subplot(3,2,4)
plot(stdaccY.date(1:end-1),stdaccY{1:end-1,["accX","accY","accZ"]},'o-')
ylabel("Std value from sensor")
legend(["X","Y","Z"],"Location","northeast")
title(compose("Vessel Acceleration: ""std"" method, %.1f seconds per window",windowlength))
subplot(3,2,5)
stackedplot(full_trip,{'roll[deg]','pitch[deg]'})
subplot(3,2,6)
gscatter(full_trip.date,full_trip.(13),full_trip.location)
0 个评论
采纳的回答
Walter Roberson
2021-10-8
编辑:Walter Roberson
2021-10-8
That error tends to occur if an axes gets deleted out from underneath you. subplot() can cause existing axes to be deleted, in the circumstance that the new axes overlaps an existing subplot without being exactly the same size.
When I look at your subplot() calls, everything looks okay to me: the only way at the moment that I could see the problem happening is if you happened to click on something while drawing was happening... in which case the "current" axes could change without notice.
You can reduce the possibilities of that happening by always expliciitly specifying the parent of graphics calls.
I do have a concern in the below: geoplot() needs a geoaxes, and I am not certain that it will play nicely with the subplot()
file = ('F:\MATLAB\Vessel_data\Galloper\Seacat Liberty\improveddata2019-09-30');
load (file)
%Create table of two columns time and acceleration
if isfield(data,'COM23_heading')== 1
full_trip = table(data.timeStamp,data.accX,data.accY,data.accZ,data.roll,data.pitch,data.yaw,data.COM23_speed,data.COM23_heading,data.COM23_lat,data.COM23_lon,data.dist2harbour,data.dist2turbine,data.inField,'VariableNames',{'timeStamp','accX[G]','accY[G]','accZ[G]','roll[deg]','pitch[deg]','yaw[deg]','speed[kph]','heading[deg]','lat','lon','dist2harbour','dist2turbine','inField'});
else
full_trip = table(data.timeStamp,data.accX,data.accY,data.accZ,data.roll,data.pitch,data.yaw,data.COM7_speed,data.COM7_heading,data.COM7_lat,data.COM7_lon,data.dist2harbour,data.dist2turbine,data.inField,'VariableNames',{'timeStamp','accX[G]','accY[G]','accZ[G]','roll[deg]','pitch[deg]','yaw[deg]','speed[kph]','heading[deg]','lat','lon','dist2harbour','dist2turbine','inField'});
end
%Convert timestamp to datenum format
full_trip.date = datetime(full_trip.timeStamp,'ConvertFrom',"datenum");
%Create time/durtion in second by converting datetime to seconds
durtime = datetime(full_trip.date, 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SS', 'Format', 'yyyy-MM-dd HH:mm:ss.SS');
durtime(isnat(durtime)) = datetime(full_trip.date(isnat(durtime)), 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
full_trip.date = durtime;
full_trip.duration = seconds(full_trip.date - full_trip.date(1));
%convert acc in G to m/s^2
full_trip.accX = convacc(full_trip.("accX[G]"), 'G''s', 'm/s^2');
full_trip.accY = convacc(full_trip.("accY[G]"), 'G''s', 'm/s^2');
full_trip.accZ = convacc(full_trip.("accZ[G]"), 'G''s', 'm/s^2');
%Rearrange and remove timestamp variable
full_trip = movevars(full_trip, {'date','duration','accX','accY','accZ'}, 'Before', "accX[G]");
%Create a categorial value of in windfarm
full_trip.location = categorical(full_trip.inField);
full_trip.location = renamecats(full_trip.location, "false", "Transit");
full_trip.location = renamecats(full_trip.location, "true", "Windfarm");
Visualize data
acctable = table(full_trip.duration, full_trip.date, full_trip.accX, full_trip.accY, full_trip.accZ, 'VariableNames', {'duration','date','accX','accY','accZ'});
acctimetable = table2timetable(acctable);
windowlength = 1800;
stdaccY = retime(acctimetable, "regular", @std, "TimeStep", seconds(windowlength));
fig = figure(1);
ax12 = subplot(3, 2, [1,2], 'Parent', fig);
geoplot(ax12, full_trip.lat, full_trip.lon)
ax3 = subplot(3, 2, 3, 'Parent', fig);
stackedplot(ax3, acctable, {'accX','accY','accZ'})
ax4 = subplot(3, 2, 4, 'Parent', fig);
h = plot(ax4, stdaccY.date(1:end-1), stdaccY{1:end-1,["accX","accY","accZ"]}, 'o-')
ylabel(ax4, "Std value from sensor")
legend(h, ["X","Y","Z"], "Location", "northeast")
title(ax4, compose("Vessel Acceleration: ""std"" method, %.1f seconds per window", windowlength))
ax5 = subplot(3, 2, 5, 'Parent' ,fig);
stackedplot(ax5, full_trip, {'roll[deg]','pitch[deg]'})
ax6 = subplot(3, 2, 6, 'parent', fig);
gscatter(ax6, full_trip.date,full_trip.(13), full_trip.location)
3 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!