Ignoring the line continuation (...) when reading a text file

2 views (last 30 days)
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
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
Stephen23
Stephen23 on 9 Dec 2022
Edited: Stephen23 on 9 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')
3.1416 It worked!
So far everything worked exactly as expected. It seems quite "autonomous" to me.

Sign in to comment.

Accepted Answer

Voss
Voss on 8 Dec 2022
Edited: Voss on 8 Dec 2022
You can rename the txt file to an m-file and run it like any other MATLAB script. The name of the m-file cannot be test.m in this case, since "test" is the name of a variable it uses. Here I rename it to test_script.m:
movefile('test.txt','test_script.m') % rename to some .m file
test_script % run the script
test = struct with fields:
data: [1×1 struct]
movefile('test_script.m','test.txt') % rename back
whos % show the workspace
Name Size Bytes Class Attributes ans 1x33 66 char cmdout 1x33 66 char test 1x1 752 struct
test.data.name % verify that the multi-line command worked ok
ans = 1×1 cell array
{'THIS IS THE NAME'}
Another option would be to read and eval() the entire file at once (instead of one line at a time):
clear % clear the old stuff, for testing the second approach
whos % verify the workspace is empty
location = 'test.txt';
FileID = fopen(location); % open the file
lines = fread(FileID,'*char').'; % read the whole file into a character vector
fclose(FileID); % close the file
eval(lines); % eval the file's contents
test = struct with fields:
data: [1×1 struct]
whos % show the workspace
Name Size Bytes Class Attributes FileID 1x1 8 double ans 1x1 8 double lines 1x172 344 char location 1x8 16 char test 1x1 752 struct
test.data.name % verify that the multi-line command worked ok
ans = 1×1 cell array
{'THIS IS THE NAME'}
  3 Comments
Voss
Voss on 8 Dec 2022
You're welcome!
Note that the first solution also does not require the "user" to do anything. The renaming and running of the file is done programmatically. Of course, you'd have to take care that the m-file to rename to doesn't already exist.
In any case, glad it's working for you!
Stephen23
Stephen23 on 9 Dec 2022
Edited: Stephen23 on 9 Dec 2022
"not requiring the user to copy paste and run anything"
The user does not have to "copy paste and run anything", you can get MATLAB to do both of those things for you. Voss even showed you exactly how, in their working example.
Why do you think that the user must "copy paste and run anything", when Voss already showed that they don't?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!