Display matrix results with message
19 views (last 30 days)
Show older comments
Tommcgtx
on 4 May 2023
Commented: Walter Roberson
on 4 May 2023
I'm new to Matlab, and just finishing up a semester of a programming class. This is only the second week of Matlab instruction. I have written a program that takes input from a file with 20 rows and one column of data. It performs calculations on that data, and creates a graph comparing the original data and the calculation, which is also stored in a matrix with one column being the original data, and the second column being the calculation. I can do all of that just fine. I can also print this output in columns to the screen. What I can't figure out is how to loop through the final output and print a message if the calculated data is above a certain value, in this case 125. So I'd like to print each row, and if the calculated data is above 125, also print a message. I had a tough time with looping in C, which was covered during most of this semester, but I figured it out as I progressed. There was a similar requirement on the last assignment which I turned in without having figured it out. Just need some guidance on how to approach this.
0 Comments
Accepted Answer
Walter Roberson
on 4 May 2023
num_rows = size(YourMatrix, 1);
for row_index = 1 : num_rows
if YourMatrix(row_index, 2) > threshold
fprintf('Alert on flight deck #%d!\n', row_index);
end
end
7 Comments
Walter Roberson
on 4 May 2023
Note that your original code did not output any text if the value was exactly 175. You had if < 175 else if > 175 -- but if the value was exactly 175 then neither of those cases would have been true and nothing would have been output. Whereas the if/else version, the else would include all cases where the first was false, so if the first is < 175 then the else would include >= 175.
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!