7.3) Forming Strings: Data Type Conversion
- You may want to form a string which includes numerical elements. The problem is that numeric variables are not strings
- Matlab has a range of functions that can convert numbers to strings (and the reverse). If we consider "A" to be a Matlab array (scalar, vector, matrix), then:
- The reverse (strings to numbers) can be useful when we want to extract numbers from strings. For example, the file name may contain numerical data that we want to use in our calculations (i.e., latitude and longitude values). See the batch processing example
numstr | convert a number to a string | str=num2str(A)num2str(A,precision)num2str(A,format) |
int2str | convert an integer to a string | str=int2str(A) |
str2num | convert a string to a number | A=str2num('str') |
str2double | convert a string to a double | A=str2double('str') |
[matlab]
>> year=2014;
>> disp(['This year is ',2014])
This year is
[/matlab]
"disp" outputs ascii strings to the display. We should have done
[matlab]
>> disp(['This year is ', num2str(year)])
This year is 2014
[/matlab]