COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### string temperature conversion

• Email
• Follow

```I'm writing a function that converts two input temperatures (in string) into Fahrenheight *IF* they are in Celsius. Otherwise, the temperatures don't change.
Some examples:

Inputs:  '54F', '95C'
Outputs: '54F, '203F'

Inputs: '78F', '56F'
Outputs: '78F', '56F'

Inputs: '23C', '56C'
Outputs: '73.4F', '132.8F'

I'm using this conversion: (celsius*9/5)+32

So far my coding is:

function [out1 out2]=temconvert(temp1, temp2)
a=double(temp1);
b=double(temp2);

Then I'm stuck. I know that you have to determine if the third position of the string is a C or F, and then see if the values need to be converted. But how do you do this? I'm confused.

Thanks a lot guys.

```
 0
Reply Raymond 9/21/2010 5:40:21 PM

See related articles to this posting

```On 10-09-21 12:40 PM, Raymond wrote:

> Then I'm stuck. I know that you have to determine if the third position
> of the string is a C or F,

if length(temp1) < 3
error('What third position??');
elseif temp1(3) == 'C'
%do whatever
elseif temp1(3) == 'F'
%do whatever
else
error('third position was not C or F')
end

Sample inputs for testing purposes should include:

'0C'
'-42C'
'100C'
'23c'
'72f'
'98'
[]
''

and, of course, no input arguments at all.
```
 0
Reply Walter 9/21/2010 5:54:10 PM

```"Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i7aqm5\$hgi\$1@fred.mathworks.com>...
> I'm writing a function that converts two input temperatures (in string) into Fahrenheight *IF* they are in Celsius. Otherwise, the temperatures don't change.
> Some examples:
>
> Inputs:  '54F', '95C'
> Outputs: '54F, '203F'
>
> Inputs: '78F', '56F'
> Outputs: '78F', '56F'
>
> Inputs: '23C', '56C'
> Outputs: '73.4F', '132.8F'
>
> I'm using this conversion: (celsius*9/5)+32
>
> So far my coding is:
>
> function [out1 out2]=temconvert(temp1, temp2)
> a=double(temp1);
> b=double(temp2);
>
> Then I'm stuck. I know that you have to determine if the third position of the string is a C or F, and then see if the values need to be converted. But how do you do this? I'm confused.
>
> Thanks a lot guys.

what is temp1 and temp2, can you post a brief example?

Oleg
```
 0
Reply Oleg 9/21/2010 5:56:05 PM

```thanks. on second thoughts, what about single digits? for example, '6C' would need to be converted as well, but then the code wouldn't be able to do it. And do I just plug in the conversion factor after each if and elseif line? Thanks.
Walter Roberson <roberson@hushmail.com> wrote in message <i7arkj\$5h3\$1@canopus.cc.umanitoba.ca>...
> On 10-09-21 12:40 PM, Raymond wrote:
>
> > Then I'm stuck. I know that you have to determine if the third position
> > of the string is a C or F,
>
> if length(temp1) < 3
>    error('What third position??');
> elseif temp1(3) == 'C'
>    %do whatever
> elseif temp1(3) == 'F'
>    %do whatever
> else
>    error('third position was not C or F')
> end
>
>
> Sample inputs for testing purposes should include:
>
> '0C'
> '-42C'
> '100C'
> '23c'
> '72f'
> '98'
> []
> ''
>
> and, of course, no input arguments at all.
```
 0
Reply Raymond 9/21/2010 6:07:21 PM

```"Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i7as8o\$26o\$1@fred.mathworks.com>...
> thanks. on second thoughts, what about single digits? for example, '6C' would need to be converted as well, but then the code wouldn't be able to do it. And do I just plug in the conversion factor after each if and elseif line? Thanks.

Well, what do YOU think YOU should do?

Walter gave you a lot of hints.

Try to solve this yourself and if you get stuck
show us what YOU have done and ask for help.

> Walter Roberson <roberson@hushmail.com> wrote in message <i7arkj\$5h3\$1@canopus.cc.umanitoba.ca>...
> > On 10-09-21 12:40 PM, Raymond wrote:
> >
> > > Then I'm stuck. I know that you have to determine if the third position
> > > of the string is a C or F,
> >
> > if length(temp1) < 3
> >    error('What third position??');
> > elseif temp1(3) == 'C'
> >    %do whatever
> > elseif temp1(3) == 'F'
> >    %do whatever
> > else
> >    error('third position was not C or F')
> > end
> >
> >
> > Sample inputs for testing purposes should include:
> >
> > '0C'
> > '-42C'
> > '100C'
> > '23c'
> > '72f'
> > '98'
> > []
> > ''
> >
> > and, of course, no input arguments at all.
```
 0
Reply someone 9/21/2010 6:17:19 PM

```If I knew, I wouldn't be asking. Please calm down. You don't have to leave a message if you have nothing to help. Thank you!
"someone" <someone@somewhere.net> wrote in message <i7asrf\$b5l\$1@fred.mathworks.com>...
> "Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i7as8o\$26o\$1@fred.mathworks.com>...
> > thanks. on second thoughts, what about single digits? for example, '6C' would need to be converted as well, but then the code wouldn't be able to do it. And do I just plug in the conversion factor after each if and elseif line? Thanks.
>
> Well, what do YOU think YOU should do?
>
> Walter gave you a lot of hints.
>
> Try to solve this yourself and if you get stuck
> show us what YOU have done and ask for help.
>
> > Walter Roberson <roberson@hushmail.com> wrote in message <i7arkj\$5h3\$1@canopus.cc.umanitoba.ca>...
> > > On 10-09-21 12:40 PM, Raymond wrote:
> > >
> > > > Then I'm stuck. I know that you have to determine if the third position
> > > > of the string is a C or F,
> > >
> > > if length(temp1) < 3
> > >    error('What third position??');
> > > elseif temp1(3) == 'C'
> > >    %do whatever
> > > elseif temp1(3) == 'F'
> > >    %do whatever
> > > else
> > >    error('third position was not C or F')
> > > end
> > >
> > >
> > > Sample inputs for testing purposes should include:
> > >
> > > '0C'
> > > '-42C'
> > > '100C'
> > > '23c'
> > > '72f'
> > > '98'
> > > []
> > > ''
> > >
> > > and, of course, no input arguments at all.
```
 0
Reply 9/21/2010 6:28:05 PM

```"Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i7atfl\$n3p\$1@fred.mathworks.com>...
> If I knew, I wouldn't be asking. Please calm down. You don't have to leave a message if you have nothing to help. Thank you!

Have you read the MATLAB "Getting Started" guide?
It is a quick read and it will answer all of your questions.
If you don't have a copy, you can read it from here:

http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

> "someone" <someone@somewhere.net> wrote in message <i7asrf\$b5l\$1@fred.mathworks.com>...
> > "Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i7as8o\$26o\$1@fred.mathworks.com>...
> > > thanks. on second thoughts, what about single digits? for example, '6C' would need to be converted as well, but then the code wouldn't be able to do it. And do I just plug in the conversion factor after each if and elseif line? Thanks.
> >
> > Well, what do YOU think YOU should do?
> >
> > Walter gave you a lot of hints.
> >
> > Try to solve this yourself and if you get stuck
> > show us what YOU have done and ask for help.
> >
> > > Walter Roberson <roberson@hushmail.com> wrote in message <i7arkj\$5h3\$1@canopus.cc.umanitoba.ca>...
> > > > On 10-09-21 12:40 PM, Raymond wrote:
> > > >
> > > > > Then I'm stuck. I know that you have to determine if the third position
> > > > > of the string is a C or F,
> > > >
> > > > if length(temp1) < 3
> > > >    error('What third position??');
> > > > elseif temp1(3) == 'C'
> > > >    %do whatever
> > > > elseif temp1(3) == 'F'
> > > >    %do whatever
> > > > else
> > > >    error('third position was not C or F')
> > > > end
> > > >
> > > >
> > > > Sample inputs for testing purposes should include:
> > > >
> > > > '0C'
> > > > '-42C'
> > > > '100C'
> > > > '23c'
> > > > '72f'
> > > > '98'
> > > > []
> > > > ''
> > > >
> > > > and, of course, no input arguments at all.
```
 0
Reply someone3 (1981) 9/21/2010 6:40:05 PM

```On 10-09-21 01:28 PM, Raymond wrote:
> If I knew, I wouldn't be asking.

If you do not have any ideas on how to generalize what I posted previously,
then you should read the Getting Started section of the Matlab documentation.

As a general statement of program development: you should not only consider
what your input "should" look like: you should also consider specifically what
input you wish to reject.

Consider for example the characters that are often seen in numbers,
'+', '-', '0' through '9', '.', 'e', 'E', and (in some contexts) 'd' or 'D';
consider also common numeric representations such as 0xA (hexadecimal), or
\045 (octal) . Which of these forms are to be allowed? Consider each of the
characters in turn: are they to be allowed as the first character of a number
for your purpose? What is allowed after that? And after that? Sanity check
your rules: if 0. is allowed and .0 is allowed, then do your rules somehow
block . by itself?

The reason for considering specifically what you want to reject is that in
doing so you will usually realize that your original rules about what to
accept were not sufficient. Over the long term, it is better to program in
terms of what you will accept rather than what you will reject: if you program
rejection and accept everything else, there is too much risk that you
overlooked some unusual case that someone will then use against you. It is
better security to only accept input you are sure you can deal with correctly
-- but you still need the stage of thinking about what you are rejecting.

As an example, if you had considered specifically what you are rejecting, then
you would have realized before that single digit numbers were not going to be
accepted under your proposed rules.
```
 0
Reply Walter 9/21/2010 6:47:47 PM

7 Replies
412 Views

Similar Articles

12/6/2013 1:40:15 PM
[PageSpeed]

 Reply:

Similar Artilces: