How do I solve this error?: Matrix out of range for deletion

1 view (last 30 days)
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)='';

Answers (1)

Andres Adam
Andres Adam on 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 Comments
Tessa Nefouse
Tessa Nefouse on 2 Aug 2024
Yes, there is a lot of info missing, I just thought it would be too much to include my entire code, so I just did this. But I am not sure what is and what isn't important to include :(. I will include the old code for when carbclay consisted of more than one value.
In my new code, I don't have a loop because carbclay is only one element, so I have deleted the 'j'. But since I reference j in the line 'legtxt{end+1} = carbclay{j}', I am struggling to figure out what to replace that with in my newer code. Does this give any more context?
[rocktypes,rocki] = unique(TAllData.gen_rock_type); %TAllData is my large data set, gen_rock_type is a column name that includes 13 different rock types
Unable to resolve the name 'TAllData.gen_rock_type'.
carbclay=[rocktypes(1),rocktypes(8),rocktypes(13)]'; %there are 13 elements in the rocktypes array, im assigning 3 of them to the category 'carbclay'
for j = 1:length(carbclay)
i = find(string(TAllData.gen_rock_type)==carbclay(j) & (TAllData.temp<=25));
if any([sum(isnan(TAllData.a_b(i)))==length(i),sum(isnan(TAllData.eff_Normal(i)))==length(i)])
missingdata1=[missingdata1,j];
else
hp(end+1) = scatter(TAllData.eff_Normal(i),TAllData.a_b(i),150,'filled','pentagram','MarkerFaceAlpha',0.7);
legtxt{end+1} = carbclay{j};
end
end
Andres Adam
Andres Adam on 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

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!