Value not adding to variable in array

5 次查看(过去 30 天)
When I run the code below and test it in my app. One of the variables in count should be 1, however, when, I display it it this is not the case. If i display the count array, for example, for f = 4. The fourth cell shows as one but if i display Count4 straight after is displays as zero. Any help would be much appreciated. Before the arrays i just had the code run for each individual set of variables (e.g. Fill1, Count 1, RAHWin, YAHWin) and the code would work as needed but turning them into arrays broke it. There seems to be a problem setting the values for count into the coutnt variable. I can attatch more code if nessecary but any help would be much appreciated.
app.Fill = {app.Fill1,app.Fill2,app.Fill3,app.Fill4,app.Fill5,app.Fill6,app.Fill7};
app.Count = {app.Count1,app.Count2,app.Count3,app.Count4,app.Count5,app.Count6,app.Count7};
app.RHWin = {app.RAHWin,app.RBHWin,app.RCHWin,app.RDHWin,app.REHWin,app.RFHWin,app.RGHWin};
app.YHWin = {app.YAHWin,app.YBHWin,app.YCHWin,app.YDHWin,app.YEHWin,app.YFHWin,app.YGHWin};
for f = 1:numel(app.Fill)
if app.Fill{f} == 1
disp(app.Count4)
app.Count{f} = app.Count{f} + 1;
app.Fill{f} = 0;
disp(app.Count{f})
if app.Past == app.Red
app.RHWin{f} = app.RHWin{f} + 1;
app.YHWin{f} = 0;
elseif app.Past == app.Yellow
app.YHWin{f} = app.YHWin{f} + 1;
app.RHWin{f} = 0;
end
end
end

采纳的回答

Walter Roberson
Walter Roberson 2022-4-3
You have a property named app.Count4 and that is completely different than your property app.Count . There is no connection between them, and you never change app.Count4 .
In particular, app.Count4 is not the same as app.Count(4), just like the 4th child of King James was not King James IV
  2 个评论
Dilan Morzaria
Dilan Morzaria 2022-4-3
编辑:Dilan Morzaria 2022-4-3
But if I’ve made app.Count and array where the fourth cell is app.Count4 then why isn’t app.Count(4) = app.Count4? I only ask cause I use app.Count4 in other parts of my code for different parts of the app, can I just use app.Count(4) instead then?
Walter Roberson
Walter Roberson 2022-4-10
Suppose you were working with books, and indexing gave you pages. Would you expect that
ScienceFictionHallOfFame(4)
would give you the same result as
ScienceFictionHallOfFame4
??
ScienceFictionHallOfFame4 is a complete name, denoting a complete book, not page 4 of the first book.
ScienceFictionHallOfFame4 is not even the 4th book in the ScienceFictionHallOfFame series -- the series went ScienceFictionHallOfFame, ScienceFictionHallOfFame2a, ScienceFictionHallOfFame2b, ScienceFictionHallOfFame3, ScienceFictionHallOfFame4 .

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by