replacing numbers with alphabets/letters in a matrix
Show older comments
I have a excel sheet (attached file) consiting of 4000 numbers (1 to 4000). I want to replace 1, 5, 9 ...3997 and 2, 6, 10...3998 with C and 3, 7, 11...3999 and 4,8, 12...4000 with H. Can a code be devised where this task can be performed such that the output file/data is represented in the same array/format as the given input? And can the code be made universal so that we can carry forth similar tasks with other arrays?
Looking forward for an answer and thank you for all your help.
Regards
J
1 Comment
Rik
on 8 Nov 2021
What have you tried yourself already?
Accepted Answer
More Answers (1)
Walter Roberson
on 8 Nov 2021
Edited: Walter Roberson
on 8 Nov 2021
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/793759/testfile_0.50.xlsx';
outfile = "testout.xlsx";
data = readmatrix(filename);
newdata = cell(size(data));
mask = isnan(data);
newdata(mask) = {nan};
mask = ismember(mod(data,4), [1 2]);
newdata(mask) = {'C'};
mask = ismember(mod(data,4), [3 0]);
newdata(mask) = {'H'};
writecell(newdata, outfile);
%testing to see if the output looks ok
readback = readtable(outfile);
readback(10:20,:)
I tested on my system, and excel does show those 0x0 char and NaN entries as empty cells.
1 Comment
ANINDYA GANGULY
on 8 Nov 2021
Categories
Find more on Spreadsheets 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!