Clear Filters
Clear Filters

textscan doesn't read the entire .m file

4 views (last 30 days)
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
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?
Phillip Maire
Phillip Maire on 9 Oct 2019
Yeah so that’s what I do. But it gets really tedious because sometimes I add more variables or comment them out. So then I’ll have saved variables that don’t apply or maybe some I forget to save.
Also if I made any error in my code that is associated with my .mat data file (i.e. if I forgot to initialize a variable and my data looks weird) I can trace that back to the code used to create it.

Sign in to comment.

Accepted Answer

Walter Roberson
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
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) = '';
Phillip Maire
Phillip Maire on 9 Oct 2019
that worked! thank you so much you are extremely helpful!

Sign in to comment.

More Answers (1)

Steven Lord
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.
  1 Comment
Phillip Maire
Phillip Maire on 9 Oct 2019
Yes this makes sense I use git hub for this. But it isn’t the same. There are no data associated with each version. I create .mat files and want the function used to create it stored with it.
To my best knowledge, source control systems don’t do that and aren’t designed to do that.
I appreciate your answer thank you

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!