Thursday, November 20, 2008

Strings: II


Some Other Useful Functions

disp

is useful to display any comment etc. on the command window as your M-file runs. It's a good idea to keep these in parts of your functions - so that you know at least till that point your code is not 'hanging'.

The syntax is:

>> disp(array)

Another use could be to, perhaps, print output in a fancy form with labels and all. An example :

>> disp(' A B C')
>> disp([1 2 3])

which gives the output:

A B C

1 2 3

warning

As the name suggests, this prints a warning (which can be suppressed). It works similar to the way disp does. This also can be helpful within M-files (say, if you want to be warned when a certain variable takes an unexpected value. Note that it does not stop the execution of the M-file. See its help for more details.


error

This prints on the output screen the custom error message and stops the execution of the M-file. See its help for more details. Works similar to warning. Another cool related command is errordlg.

num2str and str2num

num2str converts an array into their string representations. This function is most useful for playing around with dates and selecting a portion of a string/number to appear in plots or as a variable or file name. 

str2num works the other way around - it converts a character array into its numeric representation. Example:

[TBA]

Related commands include fprintf, int2str & str2int and double2str & str2double.

No comments: