Changing names within a data

1 view (last 30 days)
Nora
Nora on 25 Oct 2013
Commented: Image Analyst on 27 Oct 2013
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?

Accepted Answer

sixwwwwww
sixwwwwww on 27 Oct 2013
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 Comments
sixwwwwww
sixwwwwww on 27 Oct 2013
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 on 27 Oct 2013
Edited: Cedric on 27 Oct 2013
@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.

Sign in to comment.

More Answers (2)

sixwwwwww
sixwwwwww on 25 Oct 2013
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 Comments
Nora
Nora on 25 Oct 2013
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 on 25 Oct 2013
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.

Sign in to comment.


Cedric
Cedric on 25 Oct 2013
Edited: Cedric on 25 Oct 2013
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 Comments
Cedric
Cedric on 27 Oct 2013
Edited: Cedric on 27 Oct 2013
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 on 27 Oct 2013
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!