How do I solve this error?: Matrix out of range for deletion
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Can someone help me to figure out why I am out of range for deletion with this? This code was first used with data that had multiple values in the matrix (i.e. "carbclay" was a matrix with 4 values), but now I'm only doing it with one value, so maybe that's why it's not working? I have gotten a lot of help with this so I don't quite understand it fully, forgive my lack of knowledge when it comes to calling things the right name, hope someone can understand what's wrong:( The goal of the code is to be able to leave off legend entries if any of the data that I want to plot is missing from my table.
carbclay=[rocktypes(1)]; missingdata1=[];
np = 0;
hp = [];
legtxt = {};
i = find(string(TAllData.gen_rock_type)==carbclay);
if any([sum(isnan(TAllData.a_b(i)))==length(i),sum(isnan(TAllData.temp(i)))==length(i)])
missingdata1=[missingdata1,i];
else hp(end+1) = scatter(TAllData.temp(i),TAllData.a_b(i),120,c1,'filled','o','MarkerFaceAlpha',0.7);
legtxt{end+1} = carbclay{1};
end
carbclay(missingdata1)=''; %The error happens here for some reason, and then the legend does not show up
legend(hp,legtxt,'Location','northeastoutside');
Error Message:
Matrix index is out of range for deletion.
Error in ScriptForPlots_RFD (line 1507)
carbclay(missingdata1)='';
0 Kommentare
Antworten (1)
Andres Adam
am 2 Aug. 2024
There is a lot of information missing in that piece of code, so it's very hard to find what is the actual issue and how to resolve it.
At a high-level glance, you are defining carbclay to be a scalar: 1 element. But you are trying to access the position "missingdata1". The position "missingdata1" is informed from examining the array "TAllData.gen_rock_type".
There is no indication that TAllData.gen_rock_type is the same length as carbclay, so that is a dangerous assignment to do.
2 Kommentare
Andres Adam
am 2 Aug. 2024
I see you want to plot as a scatter the values of TAllData that are also in carbclay, and that do not give NaN in certain properties as defined by the first if clause.
You are currently wondering how to change the code to consider the case where carbclay is a scalar.
Short answer: you do not have to change anything. If the code runs OK for carbclay as an array, I don't see why it won't be applicable to arrays of length 1.
Longer answer: if you want to restructure the code anyway, legtxt and carbclay are scalars, they do not need to have {}. Also watch out for missingdata1. It is not being allocated before its use inside the double loop.
Additionally: your need to store text as legtxt might be unnecessary. Consider using the "DisplayName" property of scatter:
hp(end+1) = scatter(TAllData.eff_Normal(i),TAllData.a_b(i),150,...
'filled','pentagram','MarkerFaceAlpha',0.7...
'DisplayName',carbclay{j});
legend
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!