Uploading an excel sheet that contains some texts

Dear there, I need a help with uploading an Excel sheet then converting everything to a matrix. My issue comes as there are some texts in the first column and each one contains space. So, when I uploaded the excel sheet, I struggled to call a text as a variable (e.g. Field_A) and its belonging numbers (e.g all the rows). For example, my table would look like the below. Please note my sheet is much more complex than the below. I need an easy way to do the following please.
% my excel sheet would look like this one. This is only an example, while mine is more compicated
Jan Feb Mar
Field A 10 90 80
Field B 20 10 70
Field C 50 10 30
Total Production 80 110 180
% after i uploaded the excel sheet, I want to create my variable as below
Field_A = [10, 90, 80]
Field_B = [20, 10, 70];
...
Total_Prouduction = [80, 110, 180]

 Accepted Answer

t = readtable(filename) ;
field_yeilds = t{1:end-1, 2:end};
total_prod = t{end, 2:end};
crosscheck = sum(field_yeilds,1) - total_prod %(should be 0)

5 Comments

Dear Walter,
I do not think that is what I need. I added an excel sheet to explain what I'm looking for. I would like to convert this excel sheet to vectors as below:
London = [34, 9, 24 ...]
Imperial_library = [54, 93, 72]
etc
My Excel sheet is much complicated than this one and I would like to convert each row to a vector with its name. Anyway idea please? thank you
Imagine that you have some experience in a particular brand of table saw
and someone comes to you and asks, "Help me remove the blade guard, to make it easier to push material in by hand."
Do you tell them, "Sure, just use a #2 Phillips on this screw here, take off the guard, and you are good to go!" ?
Or do you tell them, "No! No! No! No! No!" ?
Naming variables dynamically is not a guarantee of programming failure, just like removing the blade guard is not a guarantee that you will cut off your little finger on your right hand... maybe you would only get what is left of your index finger on your left hand this time.
As Walter suggested dynamically naming your workspace variables is a bad idea. It would be a lot easier for you to work with the data if you keep it in a table. Looking at the file it seems like the data in your file is transposed, so you can read it into a table and then use rows2vars so that your desired variables become variables in your table and have your desired names.
>> t = readtable('Test_MATLAB.xlsx','NumHeaderLines',1,'ReadRowNames',true);
>> t = rows2vars(t,'VariableNamingRule','preserve')
t =
5×5 table
OriginalVariableNames London Coffee Imperial Library Hyde Park (hrs) London/G Airport
_____________________ _____________ ________________ _______________ ________________
{'Var1'} 34 54 95 68
{'Var2'} 9 93 62 97
{'Var3'} 24 72 73 74
{'Var4'} 62 54 79 2
{'Var5'} 67 53 11 73
>> t.OriginalVariableNames = [] % Remove the original variable names, since you dont need those.
t =
5×4 table
London Coffee Imperial Library Hyde Park (hrs) London/G Airport
_____________ ________________ _______________ ________________
34 54 95 68
9 93 62 97
24 72 73 74
62 54 79 2
67 53 11 73
You can then access your variables as follows
>> t.("London Coffee")
ans =
34
9
24
62
67
Dear@Walter Roberson thank you so much for your help. I agree with you but wanted a way as I have many excel sheet to deal with. Dear @Siddharth Bhutiya thank you so much for sharing this. I will try it on my code and ask some questions. I'm sorry for my late response as I have the matlab installed in a different machine which I cant access all the time.
The more excel sheets you have to deal with, the more likely you are to accidentally clobber some variable you were using for something else.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!