Export a matrix variable "losslessly" as a .txt file
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Apparently very simple issue but I was not able to find the answer anywhere!
The variable is a matrix and I wish to save it losslessly i.e. without any approximations as a .txt file.
EDIT: To clear up confusion, I have attached a sensor to my USRP B210. I want to process the data and then save the data matrix in a .txt file. I have attached how the data looks like.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/330761/image.png)
4 comentarios
Stephen23
el 12 de Jul. de 2020
"I want to process the data and then save the data matrix in a .txt file."
Sure, just use any of the text saving routines, e.g. dlmwrite, writematrix, etc.
"I have attached how the data looks like"
Your screenshot shows data displayed in the MATLAB Variable Viewer. How numeric data are displayed is not the same as what is stored in memory, so it is unclear how this helps us understand what you want.
"To clear up confusion"
I asked what you mean by "lossless" and you have not yet provided any answer.
I also gave a simple example and asked how it should be printed, a response would be useful.
Respuestas (1)
Walter Roberson
el 11 de Jul. de 2020
If it is numeric, then dlmwrite() with '%.16g'
If I recall correctly, save -double uses 15 digits instead of 16, but you could experiment to be sure, since save -double is easier to code.
3 comentarios
Walter Roberson
el 12 de Jul. de 2020
ramp = [double(time),double(real(amplitude)),double(imag(amplitude))];
save Compiled_data.txt ramp -ascii
This will generate 3 columns; when you read the data back in you would need to splice the real and imaginary back together.
If you really need "two columns" then you could fprintf() something that tried to put the real and imaginary together with an "i" after the second part, but that gets a bit risky: too much chance that a space would be emitted along the way, leaving you with three columns in some rows.
So if you really need "two columns" then the easiest would be to define columns in terms of comma delimited or something like that instead of whitespace delimited. But note that if you do that, that such as csv would not be compatible with Excel: you need an add-on to Excel to handle complex values, and the add-on handles them by constructing formulas for each cell, something like =COMPLEX(real,imaginary) .
ramp = [double(time),double(real(amplitude)),double(imag(amplitude))];
fid = fopen('Compiled_data.txt', 'wt');
fprintf(fid, '%.6f,%d%+di\n', ramp.'); %transpose is important
fclose(fid);
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!