How can I combine data from multiple files and save as one file

35 visualizzazioni (ultimi 30 giorni)
I have short script that allows me to automatically plot but I can't combine them and save them as one x,y coloumn file. Can somebody help me?
myDir = uigetdir;
myFiles = dir(fullfile(myDir,'*.txt'));
Fig = figure('visible', 'off');
for i = 1:length(myFiles)
fileName = myFiles(i).name;
data = dlmread(fullfile(myDir,fileName),'\t',1,0);
PV=data(:,1);
WA=data(:,3);
plot(PV, WA, 'Color', [0 0.4470 0.7410]);
hold on
clearvars data PV WA
end
print(Fig,fullfile(myDir,strcat('Fig_',fileName(1:end-9),'.fig')),...
'-djpeg','-r300');
fprintf('\n- Number of .txt files: %s.\n',num2str(i))
Above works.. but then I can't combine these data and plot as one file with 2 coloumb data (so I can edit them in another software such as origin)
I tried below but does not work..
open filename.fig
a = get(gca,'Children');
xdata = get(a, 'XData');
ydata = get(a, 'YData');
Also if I try to open save fig files manually from command savefig, it is simply all broken letters OR get error message 'Not a binary MAT-file. Try load -ASCII to read as text.'
Is there any simple command that allows me to combine and have one clean text file? Can somebody help me?
  2 Commenti
Santosh Gnanasekaran
Santosh Gnanasekaran il 23 Mag 2019
There is an option of combining mutiple pdf using the below command
append_pdfs('Test_Plot.pdf', pdfFile.name);
You could also try "append" option in MATLAB
Jan
Jan il 23 Mag 2019
@Santosh Gnanasekaran: append_pdfs is not a function of Matlab's toolboxes. Maybe you mean https://www.mathworks.com/matlabcentral/fileexchange/31215-append_pdfs
@sherbet ice: You confuse data, graphics, fig-files and data files massively. Please explain clearly, what yopu want to achieve. FIG files are binary MAT files, so they do not contain "broken letters", but compressed data.

Accedi per commentare.

Risposte (1)

Jan
Jan il 23 Mag 2019
If you want to join the contents of files, creating a figure is a useless indirection. Better join the files instead.
Do the files contain headers?
outFID = fopen(fullfile(myDir, 'Output.txt'), 'w');
for i = 1:length(myFiles)
fileName = myFiles(i).name;
data = fileread(fullfile(myDir,fileName));
if i == 1
fwrite(outFID, data, 'char');
else % Skip first line:
dataC = strsplit(data, char(10));
fprintf(outFID, '%s\n', dataC{2:end});
end
end
fclose(outFID);

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by