Display matrix results with message

2 views (last 30 days)
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.

Accepted Answer

Walter Roberson
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
Tommcgtx
Tommcgtx on 4 May 2023
I don't think the condition exists for exactly equal to 175 in this case. basically the idea is that a pipe crossing through a river can only take so much drag force, so if it exceeds 175N I needed to indicate that. I really appreciate your insight.
Walter Roberson
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.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!