Can the writematrix command in matlab specify the number of decimal places? - matlab

The writematrix command can write matrices to text files. For example, for a matrix like the following
A = [1, 2, 3, 4];
It can be written to file a.txt using the writematrix command
A = [1, 2, 3, 4];
writematrix(A, 'a.txt', 'WriteMode', 'append', 'Delimiter', ' ');
The output is
1 2 3 4
But if I want the output like below
1.0000 2.0000 3.0000 4.0000
How do I go about using writematrix?

Convert A into a character array with num2str in the required format before using writematrix i.e.
writematrix(num2str(A,'%.4f '), 'a.txt', 'WriteMode', 'append', 'Delimiter', 'tab');

Related

reading in a file with textscan and ignoring certain lines

I have an input file which has rows of integers like this:
1 2 3
4 5 6
7 8 9
I want to read in the file, I have used the textscan function for this kind of task before.
But there are a few lines in the data (at random positions) which contain double numbers, for example
<large number of integer lines>
0.12 12.44 65.34
<large number of integer lines>
When reading in the file, I want to ignore these lines. What's the best approach to do this? Can I tell textscan to ignore certain patterns?
The formatSpec argument could be the one you're searching for:
http://www.mathworks.de/de/help/matlab/ref/textscan.html#inputarg_formatSpec
It terminates the reading, if the content does not match the given format. If you call textscan a second time with the same file, it has to start reading where it last terminated.
From linked site:
If you resume a text scan of a file by calling textscan with the same
file identifier (fileID), then textscan automatically resumes reading
at the point where it terminated the last read.
One option is to simply just read everything in as floats - use either textscan or if your data is all numeric dlmread or similar might be simpler.
Then just remove the lines you don't want:
data =
1.0000 2.0000 3.0000
4.0000 5.0000 6.0000
0.1200 12.4400 65.3400
7.0000 8.0000 9.0000
data(data(:,1)~=round(data(:,1)),:)=[]
data =
1 2 3
4 5 6
7 8 9
If your later code requires that the type of your data matrix is non-float, use uint8 or similar to convert at this point.
Assuming that you don't know the location and number of the lines with floats, and that you don't want lines such as 1.0 2.0 3.0 or 1 2 3.0 my idea would be to read the file line by line and not store lines which contain a . character.
fid = fopen('file.txt');
nums = [];
line = fgetl(fid);
while line ~= -1 % #read until end of file
if isempty(strfind(line, '.'))
line = textscan(line, '%d %d %d');
nums = [nums; line{:}];
end
line = fgetl(fid);
end
fclose(fid);
nums is the matrix containing your data.

Specify decimal separator for .dat file in matlab [duplicate]

This question already has answers here:
Matlab: How to read in numbers with a comma as decimal separator?
(4 answers)
Closed 9 years ago.
I've got a bunch of .dat files, where the decimal separator is comma instead of dot. Is there any function in MATLAB to set comma as the separator?
You will have to read the data in as text (with textscan, textread, dlmread, etc.) and convert to numeric.
Say you have read the data into a cell array with each number in a cell:
>> C = {'1,2345','3,14159','2,7183','1,4142','0,7071'}
C =
'1,2345' '3,14159' '2,7183' '1,4142' '0,7071'
Use strrep and str2double as follows:
>> x = str2double(strrep(C,',','.'))
x =
1.2345 3.1416 2.7183 1.4142 0.7071
For your example data from comments, you have a file "1.dat" formatted similarly to:
1,2 3,4
5,6 7,8
Here you have a space as a delimiter. By default, textscan uses whitespace as a delimiter, so that is fine. All you need to change below is the format specifier for the number of columns in your data by repeating %s for each column (e.g. here we need '%s%s' for two columns):
>> fid = fopen('1.dat','r');
>> C = textscan(fid,'%s%s')
C =
{2x1 cell} {2x1 cell}
>> fclose(fid);
The output of textscan is a cell array for each column delimited by whitespace. Combine the columns into a single cell array and run the commands to convert to numeric:
>> C = [C{:}]
C =
'1,2' '3,4'
'5,6' '7,8'
>> x = str2double(strrep(C,',','.'))
x =
1.2000 3.4000
5.6000 7.8000

Reading Complex DATA in MATLAB from a File

I want to read COMPLEX data present in a txt file into 1D array in MATLAB. I'm novice in MATLAB. Please help me out.
Excuse me for the ambiguous question. Sincere Apologies..
Actually I have a .txt file containing strings that represents complex numbers(Single line in .txt file represents ) for eg:
1+3i
i
-1-i
.
.
.
So on...
I want to read the same in 1 * N Matrix (where N represents number of complex numbers in .txt file) in MATLAB. How I may do so?
I assume you mean you have a text file with two columns, the real and imaginary parts. In which case, you can do this:
>> type cplx.txt % real and imaginary parts are two columns
1 2
3 4
5 6
>> rawData = dlmread('cplx.txt')
rawData =
1 2
3 4
5 6
>> complexData = complex(rawData(:, 1), rawData(:, 2))
complexData =
1.0000 + 2.0000i
3.0000 + 4.0000i
5.0000 + 6.0000i
EDIT
Ok, with that file format, you should be able to use TEXTSCAN.
>> type cplx2.txt
1+3i
1i
2
4-4i
>> fid = fopen('cplx2.txt', 'rt');
>> x = textscan(fid, '%f');
>> fclose(fid);
>> x{1}
ans =
1.000000000000000 + 3.000000000000000i
0.000000000000000 + 1.000000000000000i
2.000000000000000 + 0.000000000000000i
4.000000000000000 - 4.000000000000000i
Note that TEXTSCAN can't handle a line that consists of 'i' on its own. Which is a shame.

importing a CSV file in MATLAB

How can I import a CSV file into MATLAB?. A row in the file i am working with looks like:
SUNW,2-Jan-98,1998,5,40.125,41.5
There are 36 columns and 10107 rows. The first row contains the column headers. It seems that MATLAB doesn't support importing such kind of CSV files. Using the following textscanfunction reads the whole data into one cell array.
data = textscan(fid, '%s %s %d %d %f %f', ...
'HeaderLines',1, 'Delimiter',',', 'CollectOutput',1);
Is there a way I could read the data into different variable for each column?
Example 6 in the textscan documentation seems to cover the use case that you're interested in:
Using a text editor, create a comma-delimited file data2.csv that
contains the lines
abc, 2, NA, 3, 4
// Comment Here
def, na, 5, 6, 7
Designate the input that textscan should treat as comments or empty
values:
fid = fopen('data2.csv');
C = textscan(fid, '%s %n %n %n %n', 'delimiter', ',', ...
'treatAsEmpty', {'NA', 'na'}, ...
'commentStyle', '//');
fclose(fid);
textscan returns a 1-by-5 cell array C with the following cells:
C{1} = {'abc'; 'def'}
C{2} = [2; NaN]
C{3} = [NaN; 5]
C{4} = [3; 6]
C{5} = [4; 7]
While it doesn't explicitly assign each column to a separate variable, you can easily do something like col1 = C{1};.
If you have MATLAB 2011b then you can use the spreadsheet import tool.

Matlab: Lost on reading weird lat lon format

I like to read in the locations of oil platforms using this file in Matlab. I obtained the file from here. "Platform.gen" looks like this:
Id Lat Lon
1 0.100000000000000D+02 0.890000000000000D+02
2 -0.941577040000000D+02 0.294488400000000D+02
3 -0.941241560000000D+02 0.292748680000000D+02
4 -0.941225830000000D+02 0.292251370000000D+02
5 -0.943647730000000D+02 0.292845940000000D+02
I read it into Matlab using:
[id lat
lon]=textread('platform.gen','%s %s
%s');
However, I am lost on how to decode the lat/lon values...help?
I would suggest instead using the conversion specifier %f to read the values. This will automatically handle the format for the double-precision floating point numbers. The character D is just another way to display scientific notation, so 0.10D+02 is 10 in double precision:
>> [id,lat,lon] = textread('platform.gen','%u %f %f','headerlines',1)
id =
1
2
3
4
5
lat =
10.0000
-94.1577
-94.1242
-94.1226
-94.3648
lon =
89.0000
29.4488
29.2749
29.2251
29.2846
Also, the function TEXTREAD will be removed in a future MATLAB version in favor of TEXTSCAN, which you could use like so:
>> fid = fopen('platform.gen','r');
>> data = textscan(fid,'%f %f %f','HeaderLines',1,'CollectOutput',true);
>> fclose(fid);
>> data{1}
ans =
1.0000 10.0000 89.0000
2.0000 -94.1577 29.4488
3.0000 -94.1242 29.2749
4.0000 -94.1226 29.2251
5.0000 -94.3648 29.2846