Add "x,y,z," to first line of .txt file in Matlab - matlab

I am working with Matlab code that writes coordinates to a text file as follows:
838959.41800,4713239.59000,1.00000,841305.41800,4708452.59000,1.00000,839175.41800,4712582.59000,1.00000
In the past, I've had to open each file individually and add the following to the first line of the .txt document:
x,y,z,
838959.41800,4713239.59000,1.00000,841305.41800,4708452.59000,1.00000,839175.41800,4712582.59000,1.00000
Any ideas on how to automate this process by embedding code which will write "x,y,z," to the first line of the Matlab output?
Thanks much,
-Aaron

Here is an example:
x = rand(1,3);
y = rand(1,3);
z = rand(1,3);
fid = fopen('data.txt','wt');
fprintf(fid, 'x,y,z,\n');
fprintf([repmat('%.6f,',[1 8]) '%.6f\n'], x,y,z)
fclose(fid);
The output file:
data.txt
x,y,z,
0.694829,0.317099,0.950222,0.034446,0.438744,0.381558,0.765517,0.795200,0.186873

Related

Using data from txt file to make a plot

I have a file with float numbers( -1.22070E-02 -7.32420E-02 ) on two columns.
I want to read them and make a plot. Something like the examples here: https://www.mathworks.com/help/signal/examples/signal-generation-and-visualization.html.
After searching this is my code:
fid = fopen('sample_b.txt', 'r');
a = fscanf(fid, '%f %f', [2 inf]);
simin = a';
plot(simin(:,1),simin(:,2));
fclose(fid);
But this is the plot I get:
And when I display simin using 'disp(simin)' I see:
81.4724 90.5792
12.6987 91.3376
63.2359 9.7540
27.8498 54.6882
I do not understand why my program uses these 8 numbers. My file contains many numbers, but the first 8 are these:
-1.22070E-02 -7.32420E-02
1.22070E-02 0.197750
4.88280E-03 9.27730E-02
-4.39450E-02 3.41800E-02
If I use 'plot(a(:,1),a(:,2));' the plot is just a line.
Edit: my data represents and EEG signal.

Issue with format specification while reading from file Matlab

I have a .dat file with a table containing data in following order:
0,000E+0 4,069E-2 -5,954E+0 1,851E-2
What I need to do is to read this data with matlab and then somehow handle it.
Here is my code:
path = 'C:/Users/user/Desktop/file1.dat';
fileID = fopen(path,'r');
formatSpec = '%e';
A = fscanf(fileID,formatSpec);
fclose(fileID);
disp(A);
Unfortunately, it doesn't work. What did I do wrong?
After replacement of comma with dot in data you can read it using dlmread function:
M = dlmread('filename', ' ');
M is what you want.
For the first part, replacing a character, you can use the following code:
% read the file
fid = fopen('input.txt','r');
f=fread(fid,'*char')';
fclose(fid);
%replace the char
f = strrep(f,',','.');
% write into the another file
fid = fopen('output.txt','w');
fprintf(fid,'%s',f);
fclose(fid);

Saving binary file in Matlab in a loop?

Consider a matrix A in Matlab of dimension mxn and suppose I want to save it as a binary file test.dat using
File_id = fopen('test.dat', 'w');
fwrite(File_id, A, 'float32');
fclose(File_id);
Now suppose that A is created within a loop for h=1:100: how can I assign to the binary files the names test1.dat, test2.dat,...,test100.dat? In other words this is what I want to do and my question is related to step 2):
%for h=1:H
%1)do something that creates A
%2) Save A using
%File_id = fopen('test'h'.dat', 'w'); %clearly wrong
%fwrite(File_id, A, 'float32');
%fclose(File_id);
%end
In the code you posted, the line:
%File_id = fopen('test'h'.dat', 'w'); %clearly wrong
should read:
File_id = fopen(strcat('test',num2str(h),'.dat'),'w');
and that should do the trick nicely.

fscanf returns empty array

I need to read data from a .dat file. There are no strings in the file, so using fscanf is valid. Below is the entirety of my code.
clear; clc;
file = 'OIL_SAMPLE1.dat';
water = fopen(file,'rt');
waterDataSize = [3 Inf];
format = '%i %d %f\n';
waterData = fscanf(water,format,waterDataSize)
fclose(water);
The file is three columns of numbers in the following format. There are 2151 lines in the .dat file.
1 300 .3
Any ideas? Thanks.

Fastest way to export a 2d matrix to a triples format CSV file in Matlab

I want to convert a 2d matrix, for example:
10 2
3 5
to a (row,col,value) CSV file, for example:
1,1,10
1,2,2
2,1,3
2,2,5
is it possible to do it in a single Matlab command?
I didn't find a way with a single command, but try the following code:
[i1,i2] = ind2sub(size(A),1:numel(A));
csvwrite('test.csv',[i2',i1',reshape(A',numel(A),1)]);
The output is:
type test.csv
1,1,10
1,2,2
2,1,3
2,2,5
Assuming A to be the input matrix, two approaches can be suggested here.
fprintf based solution -
output_file = 'data.txt'; %// Edit if needed to be saved to a different path
At = A.'; %//'
[y,x] = ndgrid(1:size(At,1),1:size(At,2));
fid = fopen(output_file, 'w+');
for ii=1:numel(At)
fprintf(fid, '%d,%d,%d\n',x(ii),y(ii),At(ii));
end
fclose(fid);
dlmwrite based approach -
At = A.'; %//'
[y,x] = ndgrid(1:size(At,1),1:size(At,2));
dlmwrite(output_file,[x(:) y(:) At(:)]);
Some quick tests seem to suggest that fprintf performs better across varying input datasizes.