My for loop calculating heading seems to be outputting only zeros to the table in app designer, not sure why
1 view (last 30 days)
Show older comments
app.comb in a combined x by 8 table. the heading table was created to store the heading calculation result of each row in the app.comb table. When the heading table is used later it only had zeros in it, suggesting the for loop wasn't read.
Not sure what the problem is, guessing it's an oversight.
app.headingtable = table('Size',[height(app.comb) 1],'VariableTypes',{'double'});
for i = 1:1:height(app.comb)
app.bow_easting = str2double(app.comb(i,3));
app.stern_easting = str2double(app.comb(i,7));
app.bow_northing = str2double(app.comb(i,4));
app.stern_northing = str2double(app.comb(i,8));
app.heading = atan2d(((app.bow_northing)-(app.stern_northing)),((app.bow_easting)-(app.stern_easting))); %heading calc using atan2
if app.heading > 0
app.headingtable(i,1) = 360 - (app.heading - 90);
elseif app.heading < 0 %if loop to bring atan2 result into heading terms
app.headingtable(i,1) = 90 - app.heading;
elseif app.heading == 0
app.headingtable(i,1) = 90;
end
end
0 Comments
Accepted Answer
Voss
on 11 Feb 2022
Edited: Voss
on 11 Feb 2022
I believe the problem is that none of the conditions in the if/elseif/elseif block are ever true, so app.headingtable doesn't get updated.
The reason none of the conditions are true is that app.heading is always NaN (and comparing NaN to 0 is always false, i.e., NaN > 0, NaN < 0, and NaN == 0 are all false).
The reason app.heading is always NaN is that app.bow_easting, app.stern_easting, app.bow_northing, app.stern_northing are all always NaN.
The reason those are NaN is because indexing a table using parentheses gives you another table (i.e., a sub-table of the table being indexed) rather than the contents of the table, and str2double() on a table returns NaN. You should use curly brace indexing to get the contents of the table.
A few simple tests follow.
(I don't know what class(es) the data in the table app.comb is, but it looks like it's probably strings or character arrays based on the fact that you use str2double() on it. If instead it is numeric class(es), then you should omit str2double(). In either case, however, you should index app.comb with curly braces {} in order to get its contents.)
data = randn(10,8);
% a table with numeric data:
comb_num = array2table(data);
comb_num
% a table with character array data:
comb_char = cell2table(arrayfun(@num2str,data,'UniformOutput',false));
comb_char
% indexing with () on a table returns another table:
comb_num(1,1)
comb_char(1,1)
% indexing with {} on a table returns the contents:
comb_num{1,1}
comb_char{1,1}
% str2double on a table returns NaN:
str2double(comb_num(1,1))
str2double(comb_char(1,1))
% str2double on a numeric type returns NaN:
str2double(comb_num{1,1})
% str2double on a string or character array is ok:
str2double(comb_char{1,1})
So the solution is to change your table indexing from () to {} (and omit str2double() if the comb table already contains numerics):
app.comb = comb_char;
app.headingtable = table('Size',[height(app.comb) 1],'VariableTypes',{'double'});
for i = 1:1:height(app.comb)
app.bow_easting = str2double(app.comb{i,3});
app.stern_easting = str2double(app.comb{i,7});
app.bow_northing = str2double(app.comb{i,4});
app.stern_northing = str2double(app.comb{i,8});
app.heading = atan2d(((app.bow_northing)-(app.stern_northing)),((app.bow_easting)-(app.stern_easting))); %heading calc using atan2
if app.heading > 0
app.headingtable{i,1} = 360 - (app.heading - 90);
elseif app.heading < 0 %if loop to bring atan2 result into heading terms
app.headingtable{i,1} = 90 - app.heading;
elseif app.heading == 0
app.headingtable{i,1} = 90;
end
end
app.headingtable
(or, in case app.comb contains numeric type data:)
app.comb = comb_num;
app.headingtable = table('Size',[height(app.comb) 1],'VariableTypes',{'double'});
for i = 1:1:height(app.comb)
app.bow_easting = app.comb{i,3};
app.stern_easting = app.comb{i,7};
app.bow_northing = app.comb{i,4};
app.stern_northing = app.comb{i,8};
app.heading = atan2d(((app.bow_northing)-(app.stern_northing)),((app.bow_easting)-(app.stern_easting))); %heading calc using atan2
if app.heading > 0
app.headingtable{i,1} = 360 - (app.heading - 90);
elseif app.heading < 0 %if loop to bring atan2 result into heading terms
app.headingtable{i,1} = 90 - app.heading;
elseif app.heading == 0
app.headingtable{i,1} = 90;
end
end
app.headingtable
And for what it's worth you probably don't need to be storing the variables bow/stern_easting/northing and heading as fields in your app structure; they can just be regular local variables since they get overwritten each time through the loop anyway. Seems like app.headingtable is the only one that needs to be used later so it can stay as a field in the app structure. For instance:
app.comb = comb_num;
app.headingtable = table('Size',[height(app.comb) 1],'VariableTypes',{'double'});
for i = 1:1:height(app.comb)
heading = atan2d(app.comb{i,4}-app.comb{i,8},app.comb{i,3}-app.comb{i,7}); %heading calc using atan2
if heading > 0
app.headingtable{i,1} = 360 - (heading - 90);
elseif heading < 0 %if loop to bring atan2 result into heading terms
app.headingtable{i,1} = 90 - heading;
elseif heading == 0
app.headingtable{i,1} = 90;
end
end
app.headingtable
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!