필터 지우기
필터 지우기

Code to edit a file name?

조회 수: 8 (최근 30일)
Karl
Karl 2011년 7월 12일
Hello all,
I'm writing a program that calibrates various files. Part of the job I want it to do is to output a new file with the calibrated data. In doing so though I want to edit the original name for the name of the output file.
Matlab seems to have a rather robust string handling system, a bit more perhaps than I'm ready to deal with at the moment. I've found that I can use the "strcat" function to jam two strings together. This will likely lead to a filename.filextensionextratxt.fileextension. But at the moment its the best I can figure out.
Anyone handy out there know of a way to perhaps remove a set number of characters at the end of a string or perhaps a more elegant method?
Thanks for your time!
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 7월 12일
You need to provide an example of your file names and the expected pattern or new file names.
B_Richardson
B_Richardson 2011년 7월 12일
strtok does just that

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

채택된 답변

Ping
Ping 2011년 7월 12일
you can call [pathstr, name, ext] = fileparts(filename) to seperate filename and extension.
  댓글 수: 1
Karl
Karl 2011년 7월 18일
Thanks for giving this suggestion Ping, I ended up having to go back and modify a different section of my code in the same program and found this particular fxn to be much easier to use. :)

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

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 7월 12일
string = 'hello world'
sm3 = string(1:end-3); %string removing three letters.

Karl
Karl 2011년 7월 12일
Hey folks, thanks so much for your helpfulness. I really did not expect so many responses so fast! Its wonderful!
For those with similar desires here is the fallout:
I originally wanted to use the fileparts command as it seemed to be a very handy way to do things (also clued me in that I could open files from the non-current folder). I had trouble though:
\Testing\Cyclohexane.csv became "\Testing" "Cyclohexane" ".csv"
When I went back and tried to writeout a file in a test using:
strcat(pathstr, name, 'added.txt');
I got:
...\Current Folder\\TestingCyclohexaneadded.txt
Obviously matlab wasn't to happy about this crazy file name with the double backslashes. I was able to fix the missing slash by just shoving an extra in the concatenation.
Ultimately I decided to just stick with the file name and use Sean de's solution to just clip off the extension and then concatenate onto the file.
filename = filename(1:end-4);
filename = strcat(filename,' extra.txt');
Which works quite nicely.
Thanks again for all the help and your time folks! Karl

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by