Table subscripting error for output of for loop that is an entire row, even though I have preallocated an empty table.

12 vues (au cours des 30 derniers jours)
Hi,
I am trying to store the output row pf the for and if loops of hotsnap_date, so that the result is a table with all the hotsnap date rows. The loops are working but just aren't storing the output in my preallocated table. I have tried hotsnap_date(z) (shows an error of table subscriting) and hotsnap_date(z,:) and hotsnapdate(z,104) as that is the no. of columns, but these show an error of mismatched no. of table variables. See code below:
hotsnap_date = array2table(zeros(n_samp_date(1),no_reef(1)+1));
for z = 1:n_samp_date(1)
disp("*************")
monthnumber = dz_sampdate.Month;
summermonths = dz_sampdate(monthnumber <= 4 | monthnumber >= 10, :);
dec = dz_sampdate(monthnumber == 12,:);
Year = year(dz_sampdate(z));
Yearbefore = year(dz_sampdate(z))-1;
if ismember(dz_sampdate(z),summermonths)
disp("summer month")
if ismember(dz_sampdate(z),dec)
disp("summer in decemeber")
hotsnap_date(z) = hotsnap(hotsnap.date == dz_sampdate(z),:);
else
disp("summer not december")
hotsnap_date(z) = hotsnap(hotsnap.date == dz_sampdate(z),:);
end
else
disp("winter month")
hotdate = string(datetime(Yearbefore,8,31));
hotsnap_date(z) = hotsnap(hotsnap.date == hotdate,:);
end
disp("done")
end
If I just have hotsnap_date = ..., the result is the last output of the for loop, showing that it is all runing correctly. But maybe it is the way I have it set up which confuses me a bit but I will copy an image to show what it looks like. To me it looks like a table inside a table, which is confusing, but this is why I tried with 2 and 104, as I was not sure what the actual number of columns would be.
TIA,
Kara
  3 commentaires
Kara Sutcliffe
Kara Sutcliffe le 2 Jan 2021
For the last z, the image I posted in the question is the output. z = 1234 in this case I am pretty sure

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 4 Jan 2021
hotsnap_date = array2table(zeros(n_samp_date(1),no_reef(1)+1));
That creates a table with variables Var1 and Var2
hotsnap(hotsnap.date == dz_sampdate(z),:)
That creates a table with however many variables hotsnap has -- a version of hotsnap with fewer rows
hotsnap_date(z) =
hotsnap_date is a table. You can never use a single subscript when assigning into a table. But using
hotsnap_date(z,:) =
will not work either, because the two tables do not have the same variables.
Your code appears to have two purposes:
  1. You emit a comment for each date about whether it is summer or not -- and you do so without emitting the date as part of the comment, so the disp() is more useful for debugging than it is for understanding the data
  2. for any date determined not to be part of summer, you replace the data with the data for August 31 of the previous year, but also without changing the date associated with the entry, so you potentially just end up with a long stream of August 31 data without any context about where it came from.
Is this correct? Because if it is, there are easier ways to accomplish those things.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by