Changing names within a data

1 次查看(过去 30 天)
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