How can I use excel file values to make matlab calculations

7 views (last 30 days)
I'm a newbie in matlab, so can anyone help me in the following question?
I want to use the data of a excel file in a matlab script.
I already import data with the following code (weather data file):
[fileName,pathname] = uigetfile({'*.xlsx'},'Select Location'); nomeficheiro=strcat(pathname,fileName); [a,b,c]=xlsread(nomeficheiro, 'A2:AJ8762'); location = c (1,2); set(handles.location_text,'String',location); In this example I get the location of the weather data using the value store in row 1 and column 2.
In this file in the column 8, we have 8760 hourly values of ambient temperature.
I need to do a calculation with all the values.
For example, import value row 1 and column 8, make calculation in matlab script, next import value row 2 and column 8, make calculation in matlab script, next import value row 3 and column 8, make calculation in matlab script,... and so on....
Thanks

Accepted Answer

Grant
Grant on 21 Jul 2015
The xlsread "raw" return value (your "c" matrix) is a cell matrix.
A cell matrix is essentially a matrix of matrices, where your string values in the excel will be 1xN character arrays, numerical values will be 1x1 double arrays, and empty cells will be a 1x1 NaN.
Spreadsheet programs will sometimes format cells containing numeric data as strings (or string is the default cell format and never changed), so you may need to use the str2num or str2double functions to perform numerical calculations.
You can index cell matrices in two ways. c(:,8) will return a Nx1 cell array, whereas c{:,8} will return N*1 values that are the contents of the embedded matrices.
You can use the function cellfun to execute a function on each cell contents. In your case, if you have a function F to process a data point, you might use the syntax
results = cellfun(@F,c(:,8))
or
results = cellfun(@(x)F(str2double(x)),c(:,8))
Where "@(x)F(str2double(x))" is an anonymous function (think lambda notation) that calls F after converting the input from a string to a double. If your result is numeric, cellfun can return a Nx1 double. If your results are not uniform, you can add the arguments "cellfun(@F,c(:,8),'Un',false)" and cellfun will return an Nx1 cell array.
You can write these values back into a spreadsheet using xlswrite.
  1 Comment
Joaquim Monteiro
Joaquim Monteiro on 22 Jul 2015
Hi Grant,
Thanks for your answer.
Maybe is a stupid questions, but like I said earlier I'm a "very" newbie in matlab (working just at 2 weeks). In my example, I don't know how to automate the calcultation. For example:
row 1 - column 8 - value=5.6, need to calculate Te = value * 2,7; row 2 - column 8 - value=5.9, need to calculate Te = value * 2,7; .... Then I want to sum the first 744 Te values corresponding to the month of January and put like Tejan = sum of 744 first values. Then do the same thing to February ... ....
Can you help me to do this?
Thank in advance

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!