I have a loop which iterates through a list of ID numbers paired with a given stress value. The code works fine, except there is no guarantee that the lists have identical lengths. I currently have an if isempty(stress_value) loop with a continue statement if an ID number doesn't have corresponding stress value. All of this takes place in a for id = 1:num_ids loop.
I am now trying to print this id value (class 'double') to the command line, if it doesn't have an assigned stress value, so if the isempty statement is True, prior to continuing out of the loop. As an example, if I set num_ids equal to 101, but the list I'm iterating through only has ID values 1-100, I want to output this 101 ID to the command line.
I've tried printing as an error like this:
error(['The following ID does not have an assigned stress value: ',id]) Here id simply prints as e however when I try this in the command window, which I don't quite understand. When I run it in the script nothing is printed to the command window.
I have also tried simply adding a display command for the id to the loop as follows, however when I run the code nothing shows up again:
disp(id) Sorry for the simple question, but I have not found an effective way to do this yet, and would appreciate your feedback!
31 Answer
Check the fprintf. You can format your output just like you want.
for id=1:num_ids % do something if isempty(stress_value) fprintf('The following ID does not have an assigned stress value: %d\n',id) continue end % do something end The error function will stop code execution.
The display function only prints the value of the variable, without printing the variable name.
2