export the data of the plot to .txt or .dat file

I have the following code and I got a plot from the data. I wanted to export the data in the plot to .txt file or a .dat file
clear
[filename, pathname] = uigetfile('*.raw;*.prc', 'Pick raw or processed data file');
N=str2double(filename(5:6));
% load processed file
fid = fopen([pathname filename],'r','b');
A= fread(fid,inf,'*single')';
prcdata=reshape(A,N,[])';
plot((1:size(prcdata,1))./1.16E6, prcdata (:,6))
fclose(fid);
% code
end
I also tried the following
csvwrite(filename,M,'','6')
figure1 = figure;
axes1 = axes('Parent',figure1)
hold(axes1,'all');
plot((1:size(prcdata,1))./1.16E6, prcdata (:,6))
saveas(figure1,'finename.jpg') % here you save the figure
title(strrep(filename,'Time','Amplitude'))

 Accepted Answer

x = (1:size(prcdata,1))./1.16E6;
y = prcdata;
xy = [x(:), y(:)];
dlmwrite('YourOutputFile.txt', xy, 'delimiter', ',');

5 Comments

got the error when I ran the code Error using horzcat Dimensions of matrices being concatenated are not consistent. Error in SJ4 (line 12) xy = [x(:), y(:)];
y should be prcdata(:, 6) so that y(:) is the same number of elements as x. Otherwise it's all the elements of y -- all columns, which will be way more than the number of rows.
thank you walter and Image analyst will test this out
This code worked it took almost an hour to analyse the data and export it.

Sign in to comment.

More Answers (1)

Well, neither of those two chunks of code does anything at all like writing the array to a text file! Try this:
filename = 'My Output file.txt'; % Whatever
fid = fopen(filename, 'wt');
if fid ~= -1
fprintf('%f\n', prcdata(:,6)); % Write out column 6 of prcdata.
fclose(fid);
else
message = sprintf('Could not open file %s for writing', filename);
uiwait(warndlg(message));
end

11 Comments

I wanted to export the data I obtained in this graph to .txt format
%% plot((1:size(prcdata,1))./1.16E6, prcdata (:,6))
when I run the code you suggested it plots the entire prcdata's column 6 Any suggestions?
Well, yeah. You plotted prcdata's column 6. You said you wanted what you plotted to go into a text file. So, the code does that. Why do I need to give another suggestion when it does exactly what you asked for. Did you also want the x values on each line of hte text file in addition to the y values?
I divided the prc data with 1.16E6 and got the graph I wanted to save the column 6 of prc data which I plotted after I divided the prc data. when I ran this code the entire prc data was being plotted
I want the x values of the graph as well to save to .txt file
x = (1:size(prcdata,1)) ./ 1.16E6;
xy = [x(:), prcdata(:,6)];
fprintf('%f, %f\n', xy); % Write out column 6 of prcdata.
is that to go with your previous code???
Yes, put those 3 lines instead of the single fprintf() from before.
the code is still running modified as you suggested... its a huge data... will let u know if it worked
This code worked it took almost an hour to analyse the data and export it.
It should not take that long unless you have hundreds of gigabytes of data.
the data I had was around 6GB that too had 5 files each... so took almost 2 days to write the whole thing into a file thank you

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!