필터 지우기
필터 지우기

textscan doesn't read the entire .m file

조회 수: 2 (최근 30일)
Phillip Maire
Phillip Maire 2019년 10월 9일
댓글: Phillip Maire 2019년 10월 9일
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
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?
Phillip Maire
Phillip Maire 2019년 10월 9일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
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.
  댓글 수: 1
Phillip Maire
Phillip Maire 2019년 10월 9일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by