How can I use excel file values to make matlab calculations
1 view (last 30 days)
Show older comments
Joaquim Monteiro
on 21 Jul 2015
Commented: Joaquim Monteiro
on 22 Jul 2015
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
0 Comments
Accepted Answer
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.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!