GUIDE editable box, Excel to Matlab Column conversion

1 view (last 30 days)
As most know Excel uses the naming of columns using A,B,C...and so forth, whereas in Matlab it is numerical 1,2,3... In my program a user can choose ANY excel document and convert it to a .mat(specifically gets the {raw} file which in turn each column will be analyzed. I cannot seem to find the code/logic that when using converts that letter to now Ex. column 2 and saves that column separately so I can make calculations on it. Another example is the user enters A into the editable text box, and the code then takes A knows it is an editable text box in GUIDE the user can enter any column from there Excel sheet Ex. B, and then the code column 1 in Matlab and gets that column and saves it separately. In addition the number of columns will not exceed Z in excel and 26 in Matlab, as well as I am analyzing cell arrays. Thank you so much!!

Accepted Answer

Geoff Hayes
Geoff Hayes on 15 Jun 2016
Tessa - if you assume that the Excel columns contain alphabetic characters in the set {A,B,C,...,Z}, then you can try using the following conversion
function [matCol] = exceToMatlab(excelCol)
numChars = length(excelCol);
matCol = 0;
for k=1:length(excelCol)
% convert the character to an integer: A->1, B->2, etc.
charAsInt = int64(upper(excelCol(k))) - 64;
% add to our running total
matCol = matCol + charAsInt * (26 ^ (numChars - k));
end
In the above, we assume that each character can be mapped to the integers 1 through 26, and then, depending upon which position the character is in, we multiply it by 26 to the exponent of that character position less one. So,
A --> 1 * 26 ^ (1 - 1) = 1 * 26^0 = 1
B --> 2 * 26 ^ (1 - 1) = 2 * 26^0 = 2
...
Z --> 26 * 26 ^ (1 - 1) = 26 * 26^0 = 26
AA --> 1 * 26 ^ (2 - 1) + 1 * 26 ^ (2 - 2) = 1 * 26^1 + 1 * 26^0 = 27
AB --> 1 * 26 ^ (2 - 1) + 2 * 26 ^ (2 - 2) = 1 * 26^1 + 2 * 26^0 = 28
etc.
Try the above and see what happens!
  5 Comments
Tessa Aus
Tessa Aus on 16 Jun 2016
Quick question there is an issue with the folder system in Matlab. I have added this to the code to save the specific column, but the issue is the data (x, columnAsNumber, columnAsString) gets saved into the file but not the Workspace. The inputs gets saved, but the only way I can find to update and import the data to the workspace is if I double click on each file separately.
% code
columnAsString = char(get(handles.CL4Latitude,'String'));
columnAsNumber = exceToMatlab(columnAsString);
% guidata(hObject,handles)
% load('columnAsNumber.mat');
set(handles.ErrorCheckBox,'string', columnAsNumber);
save columnAsNumber;
% value2 = evalin('base',columnAsNumber);
x = raw(:,columnAsNumber);
save x;
end
Geoff Hayes
Geoff Hayes on 16 Jun 2016
Tessa - are you trying to save the file with a particular name? I see that you have commented out
load('columnAsNumber.mat');
Were you trying to load some file like 1.mat where this corresponds to column A of your Excel file? I would create the filename as
filename = sprintf('%d.mat',columnAsNumber);
and then use it however you see fit (to save some data to, or load some data from). Like
filename = sprintf('%d.mat',columnAsNumber);
x = raw(:,columnAsNumber);
save(filename,'x');

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB 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!