How do you output both variable values and names in a function?

2 views (last 30 days)
I'm writing a code that converts epoch time to something akin to date time. It gives me a 2 digit year, day of the year, and fraction of the day. I'm working on converting the fraction of the day into hours, minutes and seconds. I've attached my code below, and my question pertains to the last line, when I have time = [t_h; t_m; t_s], is it possible to add something so I can differentiate hours, minutes, and seconds? When I call the function in my script I want to leave it without a semi colon so that I don't have to overcomplicate my code with additional lines using disp or fprintf because this is just an intermediate step.
function [year, day, time] = Epoch_calc(recorded_date)
%EPOCH_CALC find year, day, and time for tle data
year = floor(recorded_date/1000);
y = (recorded_date/1000 - floor(recorded_date/1000))*1000;
day = floor(y);
x = (y - floor(y))*86400;
t_h = floor(x/3600);
t_m = floor((x - t_h*3600)/60);
t_s = (x - t_h*3600 - t_m*60);
time = [t_h; t_m; t_s];
end
  2 Comments
Paul
Paul on 21 Nov 2023
Hi Bryce,
Can you clarify what "differentiate hours, minutes, and seconds" means? As it stands, I don't understand how that relates to whether or not time is defined as a column vector (with semicolons) or a row vector (without semicolons), assuming that t_h, t_m, and t_s are scalars.
Cris LaPierre
Cris LaPierre on 21 Nov 2023
Could your provide an example of what your input data is, and what the correct date should be (i.e. what is the epoch)?
I'd be tempted to use a datetime. You can then use the timofday function, the hms function, or the hour, minute and second functions to extract the information without needing to write a spearate function.

Sign in to comment.

Answers (1)

Chunru
Chunru on 21 Nov 2023
You can consider to use struct which has field names.
t = Epoch_calc(20231121)
t = struct with fields:
h: 23 m: 59 s: 59.9999
function t = Epoch_calc(recorded_date)
%EPOCH_CALC find year, day, and time for tle data
year = floor(recorded_date/1000);
y = (recorded_date/1000 - floor(recorded_date/1000))*1000;
day = floor(y);
x = (y - floor(y))*86400;
t.h = floor(x/3600);
t.m = floor((x - t.h*3600)/60);
t.s = (x - t.h*3600 - t.m*60);
% time = [t_h; t_m; t_s];
end

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!