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

조회 수: 2 (최근 30일)
Bryce Leonard
Bryce Leonard 2023년 11월 21일
댓글: Cris LaPierre 2023년 11월 21일
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
Paul
Paul 2023년 11월 21일
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 2023년 11월 21일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Chunru
Chunru 2023년 11월 21일
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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by