필터 지우기
필터 지우기

Changing names within a data

조회 수: 4 (최근 30일)
Nora
Nora 2013년 10월 25일
댓글: Image Analyst 2013년 10월 27일
I have a data file that has information like this:
Aerospace 201 Mechanical 66
And I am trying to create a script that would have the first TWO letters upper case and then the number follows so that the file name would be:
AE 201 ME 66
How do I make a script to change the name as so?

채택된 답변

sixwwwwww
sixwwwwww 2013년 10월 27일
Dear Nora, here is the code which performs your desired task:
ID = fopen('filename.txt');
a = textscan(ID, '%s%f');
fclose(ID);
b = a{1};
c = a{2};
newcell = cell(2, length(a{1}));
for i = 1:length(b)
if length(b{i}) >=2
newcell{1, i} = upper(b{i}(1:2));
else
newcell{1, i} = upper(b{i});
end
newcell{2, i} = c(i);
end
ID1 = fopen('new_filename.txt', 'w');
fprintf(ID1, '%s\t%0.f\t', newcell{:});
fclose(ID1);
I hope it helps. Good luck!
  댓글 수: 3
sixwwwwww
sixwwwwww 2013년 10월 27일
You are welcome. It is just scanning the text with respect to format specifiers "%s" for string and "%f" for numbers and storing them in a cell array which is then further processed for your desired task. Good luck!
Cedric
Cedric 2013년 10월 27일
편집: Cedric 2013년 10월 27일
@Nora: don't forget to [ Accept ] the answer that is most useful to you. You have 0% accept rate and 0 vote up, yet people spent time writing answers to your questions and comments with valuable information.

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

추가 답변 (2개)

sixwwwwww
sixwwwwww 2013년 10월 25일
Dear Nora, you can do it the following way:
ID = fopen('filename.txt');
a = textscan(ID, '%s%f%s%f');
fclose(ID);
a{1:5:length(a)} = 'AE';
a{3:5:length(a)} = 'ME';
ID1 = fopen('filename.txt', 'w');
fprintf(ID1, '%s\t%.0f\t', a{:});
fclose(ID1);
I hope it helps. Good luck!
  댓글 수: 2
Nora
Nora 2013년 10월 25일
How would I be able to do this for a dat file that has multiple rows and so it needs to loop through each one making the new name the first two letters upper case and keeping the number as is?
Matt Kindig
Matt Kindig 2013년 10월 25일
Can you post the first several lines of the dat file? I suspect that you can do this easily using regular expressions to do the replacement.

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


Cedric
Cedric 2013년 10월 25일
편집: Cedric 2013년 10월 25일
You can proceed as follows, assuming that classes are initially stored in file classes.txt and that you want to create file classes_short.txt with short names.
To read and replace:
short = regexprep( fileread('classes.txt'), '([^\d\s]{2})\S+', '${upper($1)}' ) ;
To output to new file:
fid = fopen( 'classes_short.txt', 'w' ) ;
fwrite( fid, short ) ;
fclose( fid ) ;
  댓글 수: 3
Cedric
Cedric 2013년 10월 27일
편집: Cedric 2013년 10월 27일
This is precisely what my answer is doing. Did you try it, what happened? You just have to update file names: replace classes.txt with the name of the source file and classes_short.txt with the name of the destination file that you want.
Image Analyst
Image Analyst 2013년 10월 27일
He says that he has a data file with "Aerospace 201 Mechanical 66" in it, so we first need to read that from the test file. Then he says "the file name would be: AE 201 ME 66" which is ambiguous. He does not explicitly say that he wants that written into a new text file. He calls that string a filename, so one might assume that some new text file is created with the filename "AE 201 ME 66.txt", but whose contents are unspecified. We can't know if "AE 201 ME 66" or if the contents of the file are "AE 201 ME 66" like Cedric and sixwwwwwwwwww assumed unless he clarifies.
Then he says "How do I make a script to change the name as so?" but doesn't explain this ambiguity. One possible explanation is that he wants to change the name of the original data file to the new filename which he wants created, "AE 201 ME 66.txt". Another explanation is that he wants to change "Aerospace 201 Mechanical 66" into "AE 201 ME 66", which is then used either as the filename itself, or the contents of a file with some other name. Again, we can't know for sure what "change the name" means unless he clarifies.

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

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by