textscan doesn't read the entire .m file
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Stephen23
2019년 10월 9일
"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?
채택된 답변
Walter Roberson
2019년 10월 9일
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
Walter Roberson
2019년 10월 9일
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) = '';
추가 답변 (1개)
Steven Lord
2019년 10월 9일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!