How do you output both variable values and names in a function?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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
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)?
답변 (1개)
Chunru
2023년 11월 21일
You can consider to use struct which has field names.
t = Epoch_calc(20231121)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!