Ignoring the line continuation (...) when reading a text file
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-12-9
编辑:Stephen23
2022-12-9
"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.
采纳的回答
Voss
2022-12-8
编辑:Voss
2022-12-8
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
movefile('test_script.m','test.txt') % rename back
whos % show the workspace
test.data.name % verify that the multi-line command worked ok
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
whos % show the workspace
test.data.name % verify that the multi-line command worked ok
3 个评论
Voss
2022-12-8
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
2022-12-9
编辑:Stephen23
2022-12-9
"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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!