Cookbook: Difference between revisions

Jump to navigation Jump to search
132 bytes added ,  11 November 2012
fix template
(fix template)
Line 82: Line 82:
outargs = nthargout (1:3, @myfunc)
outargs = nthargout (1:3, @myfunc)
</syntaxhighlight>}}
</syntaxhighlight>}}
=== Create a text table with fprintf===
(a.k.a. A funny formatting trick with fprintf found by chance)
Imagine that you want to create a text table with fprintf with 2 columns of 15 characters width and both right justified. How to do this thing?
That's easy:
If the variable Text is a cell array of strings (of length <15) with two columns and a certain number of rows, simply type for the kth row of Text
{{Code||<syntaxhighlight lang="octave" style="font-size:13px">
fprintf('%15.15s | %15.15s\n', Text{k,1}, Text{k,2});
</syntaxhighlight>}}
The syntax '%<n>.<m>s' allocates '<n>' places to write chars and display the '<m>' first characters of the string to display.
Example:
{{Code|Example create a text table with fprintf|<syntaxhighlight lang="octave" style="font-size:13px">
octave:1> Text={'Hello','World'};
octave:2> fprintf('%15.15s | %15.15s\n', Text{1,1}, Text{1,2})
          Hello |          World
</syntaxhighlight>}}
===Load comma separated values (*.csv) files===
{{Code|Load comma separated values files|<syntaxhighlight lang="octave" style="font-size:13px">
A=textread("file.csv", "%d", "delimiter", ",");
B=textread("file.csv", "%s", "delimiter", ",");
inds = isnan(A);
B(!inds) = num2cell(A(!inds))
</syntaxhighlight>}}
This gets you a 1 column cell array. You can reshape it to the original size by using the <code>reshape</code> function
The next version of octave (3.6) implements the <code>CollectOutput</code> switch as seen in example 8 here:                    http://www.mathworks.com/help/techdoc/ref/textscan.html
Another option is to use the function <code>csvread</code>, however this function can't handle non-numerical data.
===Using variable strings in commands===
For example, to plot data using a string variable as a legend:
Option 1 (simplest):
{{Code|Using variable strings in commands. op1|<syntaxhighlight lang="octave" style="font-size:13px">
legend = "-1;My data;";
plot(x, y, legend);
</syntaxhighlight>}}
Option 2 (to insert variables):
{{Code|Using variable strings in commands. op2|<syntaxhighlight lang="octave" style="font-size:13px">
plot(x, y, sprintf("-1;%s;", dataName));
</syntaxhighlight>}}
Option 3 (not as neat):
{{Code|Using variable strings in commands. op3|<syntaxhighlight lang="octave" style="font-size:13px">
legend = 'my legend';
plot_command = ['plot(x,y,\';',legend,';\')'];
eval(plot_command);
</syntaxhighlight>}}
These same tricks are useful for reading and writing data files with unique names, etc.


== Mathematics ==
== Mathematics ==
=== Test if a number is a integer ===
There's several menthods to do this. The simplest method is probably {{Codeline|<nowiki>fix (x) == x</nowiki>}}
=== Find if a number is even/odd ===
=== Find if a number is even/odd ===
==== Problem ====
==== Problem ====
Line 191: Line 254:
get (0, "screensize")
get (0, "screensize")
</syntaxhighlight>}}
</syntaxhighlight>}}
== Input/Output ==
=== Create a text table with fprintf===
(a.k.a. A funny formatting trick with fprintf found by chance)
Imagine that you want to create a text table with fprintf with 2 columns of 15 characters width and both right justified. How to do this thing?
That's easy:
If the variable Text is a cell array of strings (of length <15) with two columns and a certain number of rows, simply type for the kth row of Text
{{Code||<syntaxhighlight lang="octave" style="font-size:13px">
fprintf('%15.15s | %15.15s\n', Text{k,1}, Text{k,2});
</syntaxhighlight>}}
The syntax '%<n>.<m>s' allocates '<n>' places to write chars and display the '<m>' first characters of the string to display.
Example:
{{Code|Example create a text table with fprintf|<syntaxhighlight lang="octave" style="font-size:13px">
octave:1> Text={'Hello','World'};
octave:2> fprintf('%15.15s | %15.15s\n', Text{1,1}, Text{1,2})
          Hello |          World
</syntaxhighlight>}}
===Load comma separated values (*.csv) files===
{{Code|Load comma separated values files|<syntaxhighlight lang="octave" style="font-size:13px">
A=textread("file.csv", "%d", "delimiter", ",");
B=textread("file.csv", "%s", "delimiter", ",");
inds = isnan(A);
B(!inds) = num2cell(A(!inds))
</syntaxhighlight>}}
This gets you a 1 column cell array. You can reshape it to the original size by using the <code>reshape</code> function
The next version of octave (3.6) implements the <code>CollectOutput</code> switch as seen in example 8 here:                    http://www.mathworks.com/help/techdoc/ref/textscan.html
Another option is to use the function <code>csvread</code>, however this function can't handle non-numerical data.
===Using variable strings in commands===
For example, to plot data using a string variable as a legend:
Option 1 (simplest):
{{Code|Using variable strings in commands. op1|<syntaxhighlight lang="octave" style="font-size:13px">
legend = "-1;My data;";
plot(x, y, legend);
</syntaxhighlight>}}
Option 2 (to insert variables):
{{Code|Using variable strings in commands. op2|<syntaxhighlight lang="octave" style="font-size:13px">
plot(x, y, sprintf("-1;%s;", dataName));
</syntaxhighlight>}}
Option 3 (not as neat):
{{Code|Using variable strings in commands. op3|<syntaxhighlight lang="octave" style="font-size:13px">
legend = 'my legend';
plot_command = ['plot(x,y,\';',legend,';\')'];
eval(plot_command);
</syntaxhighlight>}}
These same tricks are useful for reading and writing data files with unique names, etc.

Navigation menu