Ignoring the line continuation (...) when reading a text file
Show older comments
Hello all! I have a text file with some structure inputs that I'm reading and evaluating in MATLAB directly using the eval function. The file content looks similar to this:
test = struct;
test.data = struct;
test.data.dummy = cell(1, 1);
test.data.dummy{1} = '1234';
test.data.name = cell(1, 1);
test.data.name{1} = ...
'THIS IS THE NAME'
and the code I'm using looks similar to this:
location = 'D:\Documents\test.txt';
FileID = fopen(location)
while ~feof(FileID)
line=fgetl(FileID)
eval(line)
end
fclose(FileID)
When it comes to the three dots (line continuation), of course the eval function doesn't understand it. How can I have the code wait and gets the value after those three dots?
Thanks
3 Comments
Mohamed AKI Ahmed
on 8 Dec 2022
"I needed it to work in the way I asked since it's connected to another program. Wanted things to be autonomous"
Getting MATLAB to copy the file and run it would be a neater approach to your original task. Lets try it right now:
% create text file:
writelines(["x=...","pi;","disp(x)","disp('It worked!')"],'mytest.txt')
% copy file (note how MATLAB does this, user does not do anything):
copyfile('mytest.txt','myscript.m')
% run file (note how MATLAB does this, user does not do anything):
run('myscript')
So far everything worked exactly as expected. It seems quite "autonomous" to me.
Accepted Answer
More Answers (0)
Categories
Find more on Large Files and Big Data 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!