Tips and tricks: Difference between revisions

Jump to navigation Jump to search
1,195 bytes removed ,  21 September 2012
m
No edit summary
Line 77: Line 77:


==General==
==General==
===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))
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


===Using Variable Strings in Octave Commands===
===Using Variable Strings in Octave Commands===
657

edits

Navigation menu