textscan doesn't read the entire .m file
2 views (last 30 days)
Show older comments
Phillip Maire
on 9 Oct 2019
Commented: Phillip Maire
on 9 Oct 2019
Hello everyone,
WHAT I want to do: Get all the lines of any given function (.m file) into a cellarray of strings. e.g. for the 'sum.m' file I want a cell that looks like...
[x] = magicFunction('sum.m')
x = {...
'%SUM Sum of elements.'...
'% S = SUM(X) is the sum of the elements of the vector X. If X is a matrix,'...
'% S is a row vector with the sum over each column. For N-D arrays, '...
'% SUM(X) operates along the first non-singleton dimension.'...
}
etc.
WHY I want to do it: I want to be able to store my function (I'm just using sum.m as an example) with the variables created from that function. This way any settings or tweaks I made on that run will be saved with the variable. This is important to me because I have to do a lot of trial and error when doing analysis (I study neural response properties) and I may forget what properties I set.
I run 10's of programs sometimes with 20 important settings I need to record. Sometimes changing one setting at a time and then running it. The number of combinations get big pretty fast.
the below code displays my problem. You can run it on your machine to see what I mean
funcName = 'sum.m';
fid=fopen(funcName);
C ={};
C2 = {};
for k = 1: 1000
C{k, 1} = textscan(fid,'%s',1,'delimiter','\r', 'headerlines',k-1);
C2{k, 1} = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',k-1);
end
fclose(fid);
for k = 1:length(C)
if ~isempty(C{k}{:})
disp(k)
end
disp(C{k}{:});
end
fprintf('\n\n\n\n')
for k = 1:length(C2)
if ~isempty(C2{k}{:})
disp(k)
end
disp(C2{k}{:});
end
edit(funcName)
I think that the "%S" in the "%SUM" in the beginning of sum.m is likely screwing me up because it is a string command??
4 Comments
Stephen23
on 9 Oct 2019
"I run 10's of programs sometimes with 20 important settings I need to record."
It is not clear from your question how those "settings" relate to the function code. Why not just store the settings themselves?
Accepted Answer
Walter Roberson
on 9 Oct 2019
You can use fileread() and splitlines().
However due to old habits I am more likely to use regexp() with the 'split' option than I am to use splitlines ()
3 Comments
Walter Roberson
on 9 Oct 2019
That does not happen for me.
I suspect your file is created in MS Windows with CR+LF line terminators.
funcString = fileread('dummy1.m');
funcString = regexprep(funcString, '\r', '');
fprintf('%s', funcString);
There are other ways of coding the deletion of the \r instead of regexrep(), such as
funcString(funcString == 13) = '';
More Answers (1)
Steven Lord
on 9 Oct 2019
Rather than trying to save the contents of your code files as variables, why not set up a source control system? That way you can check each version of your code into the system and go back to any previously checked-in version of the file? You could check in a MAT-file containing the results at the same time, so you'll have traceability and time consistency.
See Also
Categories
Find more on Characters and Strings 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!