MATLAB: Trouble formating strings with fprintf.

fprintfhelphomework

Hello, I am working on an assignment in which the user is prompted if they work on a certain day and if they are they enter the time that they are working. At the end of the code we have to print a table with the days, start, and end. I tried experimenting to see if one fprintf statement would work better than multiple but I did not have luck with that. Usually increasing the field straightens the columns but in this case I'm having trouble understanding how to format this table so they are all aligned. Is this something that is possible with fprintf. Any help would be greatly appreciated. Thanks.
disp('This program will write up your working schedule')
% Sunday
sunday = menu('Are you working Sunday?','Yes','No');
if sunday == 1
sunday_start = input('Enter your start time for Sunday in the format (e.g 12:30 am/pm): ', 's');
sunday_end = input('Enter your end time for Sunday in the format (e.g 12:30 am/pm): ', 's');
else
sunday_start = "Off";
sunday_end = "Off";
end
% Monday
monday = menu('Are you working Monday?','Yes','No');
if monday == 1
monday_start = input('Enter your start time for Monday in the format (e.g 12:30 am/pm): ', 's');
monday_end = input('Enter your end time for Monday in the format (e.g 12:30 am/pm): ', 's');
else
monday_start = "Off";
monday_end = "Off";
end
% Tuesday
tuesday = menu('Are you working Tuesday?','Yes','No');
if tuesday == 1
tuesday_start = input('Enter your start time for Tuesday in the format (e.g 12:30 am/pm): ', 's');
tuesday_end = input('Enter your end time for Tuesday in the format (e.g 12:30 am/pm): ', 's');
else
tuesday_start = "Off";
tuesday_end = "Off";
end
% Wednesday
wednesday = menu('Are you working Wednesday?','Yes','No');
if wednesday == 1
wednesday_start = input('Enter your start time for Wednesday in the format (e.g 12:30 am/pm): ', 's');
wednesday_end = input('Enter your end time for Wednesday in the format (e.g 12:30 am/pm): ', 's');
else
wednesday_start = "Off";
wednesday_end = "Off";
end
% Thursday
thursday = menu('Are you working Thursday?','Yes','No');
if thursday == 1
thursday_start = input('Enter your start time for Thursday in the format (e.g 12:30 am/pm): ', 's');
thursday_end = input('Enter your end time for Thursday in the format (e.g 12:30 am/pm): ', 's');
else
thursday_start = "Off";
thursday_end = "Off";
end
% Friday
friday = menu('Are you working Friday?','Yes','No');
if friday == 1
friday_start = input('Enter your start time for Friday in the format (e.g 12:30 am/pm): ', 's');
friday_end = input('Enter your end time for Friday in the format (e.g 12:30 am/pm): ', 's');
else
friday_start = "Off";
friday_end = "Off";
end
% Saturday
saturday = menu('Are you working Saturday?','Yes','No');
if saturday == 1
saturday_start = input('Enter your start time for Saturday in the format (e.g 12:30 am/pm): ', 's');
saturday_end = input('Enter your end time for Saturday in the format (e.g 12:30 am/pm): ', 's');
else
saturday_start = "Off";
saturday_end = "Off";
end
disp('Your work schedule is below')
fprintf('\tDays Start End \n')
fprintf('\tSunday %20s %20s \n', sunday_start, sunday_end)
fprintf('\tMonday %20s %20s \n', monday_start, monday_end)
fprintf('\tTuesday %20s %20s \n', tuesday_start, tuesday_end)
fprintf('\tWednesday %20s %20s \n', wednesday_start, wednesday_end)
fprintf('\tThursday %20s %20s \n', thursday_start, thursday_end)
fprintf('\tFriday %20s %20s \n', friday_start, friday_end)
fprintf('\tSaturday %20s %20s \n', saturday_start, saturday_end)
% Experimenting
starts = [sunday_start monday_start tuesday_start wednesday_start thursday_start friday_start saturday_start];
ends = [sunday_end monday_end tuesday_end wednesday_end thursday_end friday_end saturday_end];
fprintf('\tSunday %20s %20s \n\tMonday %20s %20s \n\tTuesday %20s %20s \n\tWednesday %20s %20s \n\tThursday %20s %20s \n\tFriday %20s %20s \n\tSaturday %20s %20s \n', [starts; ends])

Best Answer

Try putting \t before every %20s, or else add spaces to the end of every day to make it as long as Wednesday.