Changing names within a data
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
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
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!
More Answers (2)
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
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.
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
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.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!