import edit and write .txt file

5 views (last 30 days)
Ersin Buyuk
Ersin Buyuk on 27 Dec 2017
I have a .txt file contains text and numeric values. I need import and get some numeric values as a matrix or string. After than some process in matlab change the numeric new values with old numeric values on same position in txt file an save as same name txt file
  1 Comment
Ersin Buyuk
Ersin Buyuk on 3 Jan 2018
Is there someone who can find the answer to your problem? I still have not get the values as a series on the matrix.

Sign in to comment.

Answers (1)

Morteza Hajitabar Firuzjaei
%write x = 0:.1:1; A = [x; exp(x)];
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
%read
% y = readfile(filename)
% Read file 'filename' and return a MATLAB string with the contents
% of the file. Internally C functions fopen/fread are used to read
% data from the file.
function y = readfile(filename) %#codegen
% The 'fprintf' function will not be compiled and instead passed
% to the MATLAB runtime. If we choose to generate code for this example,
% all calls to extrinsic functions are automatically eliminated. coder.extrinsic('fprintf');
% Put class and size constraints on function input.
assert(isa(filename, 'char'));
assert(size(filename, 1) == 1);
assert(size(filename, 2) <= 1024);
% Define a new opaque variable 'f' which will be of type 'FILE *'
% in the generated C code initially with the value NULL.
f = coder.opaque('FILE *', 'NULL');
% Call fopen(filename 'r'), but we need to convert the MATLAB
% string into a C type string (which is the same string with the
% NUL (\0) string terminator).
f = coder.ceval('fopen', c_string(filename), c_string('r'));
% Call fseek(f, 0, SEEK_END) to set file position to the end of
% the file.
coder.ceval('fseek', f, int32(0), coder.opaque('int', 'SEEK_END'));
% We need to initialize the variable 'filelen' to the proper type
% as custom C functions are not analyzed.
filelen = int32(0);
% Call ftell(f) which will return the length of the file in bytes
% (as current file position is at the end of the file).
filelen = coder.ceval('ftell', f);
% Reset current file position
coder.ceval('fseek', f, int32(0), coder.opaque('int', 'SEEK_SET'));
% Initialize a buffer
buffer = zeros(1,65536,'uint8');
% Remaining is the number of bytes to read (from the file)
remaining = filelen;
% Index is the current position to read into the buffer
index = int32(1);
while remaining > 0
% Buffer overflow?
if remaining + index > size(buffer,2)
fprintf('Attempt to read file which is bigger than internal buffer.\n');
fprintf('Current buffer size is %d bytes and file size is %d bytes.\n', size(buffer,2), filelen);
break
end
% Read as much as possible from the file into internal buffer
nread = coder.opaque('size_t');
nread = coder.ceval('fread', coder.ref(buffer(index)), int32(1), remaining, f);
n = int32(0);
n = coder.ceval('(int)',nread);
if n == 0
% Nothing more to read
break;
end
% Did something went wrong when reading?
if n < 0
fprintf('Could not read from file: %d.\n', n);
break;
end
% Update state variables
remaining = remaining - n;
index = index + n;
end
% Close file
coder.ceval('fclose', f);
y = char(buffer(1:index));
% Create a NUL terminated C string given a MATLAB string
function y = c_string(s)
y = [s 0];
Morteza Hajitabar Firuzjaei

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!