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?
0 个评论
采纳的回答
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
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!
更多回答(2 个)
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 个评论
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
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 个评论
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 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!