Filter löschen
Filter löschen

Problems with using fprintf for writing data to a text file

3 Ansichten (letzte 30 Tage)
Martijn Bregman
Martijn Bregman am 6 Apr. 2017
Kommentiert: dpb am 6 Apr. 2017
Hello everyone,
I'm currently working on a data transformation of multiple kinds of input data. I developed multiple scripts which stores relevant data in a specific way to a structure in Matlab. The next step is writing this data back to a text file which is suitable for the software which is used to analyse the data. I'm using the following function for this:
function F_writeJRK
global data
global file
global ds
%Open the new file in the OutputFiles folder based on the name given by the user
fid_res=fopen([fullfile(pwd,'\OutputFiles',file.dest_filename) '.jrk'],'w');
for o=1:ds %loop through all the subdatasets
header=data.metadata{o,1}; %Accommodate header data
formatSpec1= '\r\n%s %s %s %s %s %s %s'; %Specify lay-out of header data
fprintf(fid_res,formatSpec1,header{1:7}); %Write header data in file
datRSP=data.measurements{o,1}(:); %Acquire RSP distances
datHeight=data.measurements{o,2}(:); %Acquire measured heights
datProfcode=data.measurements{o,3}(:); %Acquire profielcodenummers
datCom=strcat(num2str(datRSP),{' '},num2str(datHeight),num2str(datProfcode)); %Combine all
formatSpec2= '\r\n %s %s %s %s %s'; %Specify lay-out of measurement data
fprintf(fid_res,formatSpec2,datCom{:}); %Write data in file
end
end
The for-loop loops through all the dataset which are present in the data.metadata and data.measurement variables. The destination file exists out of many subdatasets with a header and subsequently the data (as defined by datCom). They are stored below eachother.
The problem is that in some cases the last subdataset is not stored entirely or not stored at all in the destination file. It is however present in the data structure. In one case only the header of the last dataset is stored, and not the data. In another case only the first 1024 or 2048 datapoints are stored, which might seems to indicate that there is some kind of storing/memory problem in Matlab? These problems only apply on the last subdataset, in case when this dataset is not the last, all datapoints are stored in the destination file.
The error seems rather weird, and I was not able to find out the source of the problem. I suspect that something goes wrong in the fprintf part, but I couldn't figure out why, and couldn't find any similar questions either.
Thank you in advance!
Kind regards,
Martijn
  1 Kommentar
dpb
dpb am 6 Apr. 2017
I'd suspect
  1. No corresponding fclose for the file; possibly it's being buffered but never completed the physical operation to disk
  2. global variables may change any part of your application; pass the structures to the function to be operated upon.
  3. If this reproducible with the same dataset I'd suspect a flaw in that particular dataset that the dereferencing operation doesn't.
IOW, I would NOT expect the problem to be fundamental to fprintf itself nor some memory limit or the like unless the file could approach the 2GB limit on a 32-bit OS or somesuch.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by