Domanda

I have this code as part of my program to calculate the solar time. But my calculation using decimal time. And now, I need to display the result in clock time.

%To calculate solar time
stime = time + (((4*(Lloc-Lsm))+ Et)/60);
disp(['The true solar time is ' num2str(stime),'hr']); %To display the solar time

The answer goes like this:

The true solar time is 13.0501hr

How can I convert the time to clock time (12hr or 24 hr format). i.e the minute should multiply by 60 (0.0501*60min = 3.006) and the time should display as 13:03 or 1:03PM.

Appreciate your help. Thank you. Regards, -researcher-

È stato utile?

Soluzione 2

Use datenum to convert to a serial date number and then datestr to build the string with appropriate format:

h = 13.0501; %// your computed decimal time
string = datestr(datenum([0 0 0 h 0 0]),14); %// or change "14" for other formats

The two formats you specify correspond to formats 13 through 16 in datestr (see link to the documentation above). For example, with format 14 the string is

>> disp(string)
 1:03:00 PM

Altri suggerimenti

you can use the following

my_hour = floor(stime);
my_minute = round(mod(stime, 1) * 60);

disp(['The true solar time is ', num2str(my_hour), ':', num2str(my_minute)]);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top