How can I use excel file values to make matlab calculations
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Joaquim Monteiro
le 21 Juil 2015
Commenté : Joaquim Monteiro
le 22 Juil 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 commentaires
Réponse acceptée
Grant
le 21 Juil 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!