How can I control the formatting when saving a matrix to a file? - matlab

I save a matrix to a file like this:
save(filepath, 'mtrx', '-ascii');
Is there a way to tell MATLAB to write 0 instead of 0.0000000e+000 values? It would be nice because it would be faster and easier to see which values differ from zero.

I suggest using DLMWRITE instead of SAVE since you're dealing with ASCII files. It will give you more control over the formatting. For example, you could create an output file delimited by spaces with a field width of 10 and 6 digits after the decimal point (see more about format specifiers here):
dlmwrite(filepath,mtrx,'delimiter',' ','precision','%10.6g');

Related

Reading text file with multiple characters and integers in MATLAB

I have a text file that contains alphabets, characters (comma, semicolon,space,asterisk etc.) and floating point numbers.How can I read this text file in MATALAB ans store it in an array/structure?
I suggest you use the textscan function.
Provided your text file is organized into columns of values sharing the same format, textscan will allow you to read each column with the appropriate conversion specifier.
Your data will be stored into a cell array.

matlab cannot read text file containing ^* as power of 10

I need to read text files into Matlab. In the text files there are numbers like 5.875489^*-6, which is indeed 0.000005875489. Matlab cannot read this format and since there are too many files, I cannot change the format in all the files manually. So, I wonder if there is any tips to make Matlab reading the files as they are?
Any help and guide is highly appreciated.
Marilla.
As pointed out by #vu1p3n0x, it would probably be easier to replace ^* by e using a replace-all. Alternatively, if that is unpractical, you could read in the the mantissa and exponent separately and perform the exponentiation in Matlab:
Raw = textscan(fid, '%f^*%f');
Result = Raw{1}.*10.^Raw{2};

Matlab textscan and date

I am a newbie in matlab and i will appreciate your help.
1,2011-01-01,1,0,1,0,6,0,2,0.344167,0.363625,0.805833,0.160446,331,654,985
This is the first raw of my dataset. 16 numeric columns except the second which is in a date format different from that in matlab documentation.
How can I include this in textscan and have 16 different columns?
You can use importdata.
Use:
A=importdata('<Filename>','<delimiter>');
In this case <Filename> is name of the file with abbreviation (Foo.txt), <delimiter> is comma ','.
A will be structure with A.data containg numeric values only, A.textdata all elements as strings. If you want to process it, use regexp.

Matlab Importdata Precision

I'm trying to use importdata for several data files containing data of a precision up to 11 digits after the decimal, is Matlab seems to think I am only interested in the first 5 digits when using importdata, is there an alternative method I could use to load my data, or a method to define the precision to which I want my data loaded?
First try:
format long g
Also, can you paste some of the data you are trying to load?

How do you save data to a text file in a given format?

I want to save a matrix to a text file, so I can read it by another program. Right now I use:
save('output.txt', 'A','-ascii');
But this saves my file as
6.7206983e+000 2.5896414e-001
6.5710723e+000 4.9800797e-00
6.3466334e+000 6.9721116e-001
5.9975062e+000 1.3346614e+000
6.0224439e+000 1.8127490e+000
6.3466334e+000 2.0517928e+000
6.3965087e+000 1.9721116e+000
But I would like to have them saved without the "e-notation" and not with all the digits. Is there an easy way to do this?
Edit: Thank you! That works just fine. Sorry, but I think I messed up your edit by using the rollback.
I would use the fprintf function, which will allow you to define for yourself what format to output the data in. For example:
fid = fopen('output.txt', 'wt');
fprintf(fid,'%0.6f %0.6f\n', A.');
fclose(fid);
This will output the matrix A with 6 digits of precision after the decimal point. Note that you also have to use the functions fopen and fclose.
Ditto gnovice's solution, if you need performance & custom formatting.
dlmwrite gives you some control (global, not per-field basis) of formatting. But it suffers from lower performance. I ran a test a few years ago and dlmwrite was something like 5-10x slower than the fopen/fprintf/fclose solution. (edit: I'm referring to large matrices, like a 15x10000 matrix)