Tips and tricks: Difference between revisions

1,725 bytes added ,  27 November 2011
+ general
(Ported C++ stuff)
 
(+ general)
Line 61: Line 61:
<tr><td>conjugate tranpose</td><td><code>A'</code></td><td><code>A.hermitian()</code></td></tr>
<tr><td>conjugate tranpose</td><td><code>A'</code></td><td><code>A.hermitian()</code></td></tr>
</table>
</table>
=General=
==How to declare functions inside a test block==
function experience
%!test
%! experience_design_mat
%! experience_obs_eqs
%! assert (experience_design_mat == pi);
%! assert (experience_obs_eqs == exp(1));
%!
%! endfunction  % this is a trick.
%! % now we can declare functions to be used by the test above.
%!
%! function a = experience_design_mat
%!    a = pi;
%! endfunction
%!
%! function b = experience_obs_eqs
%!    b = exp(1);
%! % endfunction: don't add it here. Let test() do it.
==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
fprintf('%15.15s | %15.15s\n', Text{k,1}, Text{k,2});
The syntax '%<n>.<m>s' allocates '<n>' places to write chars and display the '<m>' first characters of the string to display.
Example:
octave:1> Text={'Hello','World'};
octave:2> fprintf('%15.15s | %15.15s\n', Text{1,1}, Text{1,2})
          Hello |          World
==Load Comma Separated Values (*.csv) files==
A=textread("file.csv", "%d", "delimiter", ",");
B=textread("file.csv", "%s", "delimiter", ",");
inds = isnan(A);
B(!inds) = num2cell(A(!inds))
B = reshape(B,
This gets you a 1 column cell array. You can reshape it to the original size by using the <code>reshape</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