Matlab questions: Read from Excel file

In summary, the person is looking for help with writing a program in Matlab that can cycle through different sets of data and apply a function to each variable separately. They also want to know if it is possible to replace a fixed number in a file name with a variable. One way to do this is by using string concatenation and the strcat function. It is also noted that as of R2019a, Matlab recommends using the readmatrix function instead of xlsread.
  • #1
pp84
4
0
Hi,
Looking for a bit of cheeky help with matlab, I am stumped!

1) I have written a program that i want to cycle through different sets of data, ideally this is what i would like:
These are all xls files with columns of data

ydata = ...
xdata = ...
zdata = ...

for variable = ydata:xdata:zdata
do function to each of the variables seperatly
write function results(variable) and save data
end

Is this possible?

2) When calling a file name eg:

[FTSE] = xlsread('Normalised Smooth Data.xlsx', 'FTSE', 'A1:A300')

I want to replace 300 with a variable rather than changing many of them manually everytime i need to change that number, so i would have something like this:

numberofpoints = 300
[FTSE] = xlsread('Normalised Smooth Data.xlsx', 'FTSE', 'A1:Anumberofpoints')

Again, is this possible? I am sure it is i just don't know the key commands.

Any help would be much appretiated.

Thanks

Punit
 
Physics news on Phys.org
  • #2
OP last seen in 2010. Anyone want to take a crack at it, anyway!
 
  • #3
One way of doing this would be to use string concatenation to construct the relevant string, i.e., do something like
Code:
N = 300;
str = strcat('A1:A',int2str(N));
You can then use the str variable instead of 'A1:A300' in your original code (or you can put the strcat and everything in its argument as the argument of xlsread directly).
 
  • Like
Likes Wrichik Basu
  • #4
Note that as of R2019a, Matlab no longer recommends the xlsread function. Use readmatrix instead.
 

FAQ: Matlab questions: Read from Excel file

1. How do I read data from an Excel file in Matlab?

To read data from an Excel file in Matlab, you can use the "xlsread" function. This function takes in the name of the Excel file and the range of cells to be read as inputs, and returns the data as a numerical array.

2. Can I read multiple sheets from an Excel file in Matlab?

Yes, you can use the "xlsread" function to read multiple sheets from an Excel file in Matlab. Simply specify the sheet name or index number as an additional input to the function.

3. How do I handle missing or empty cells when reading from an Excel file in Matlab?

By default, the "xlsread" function replaces missing cells with the "NaN" value. However, you can specify the "basic" or "transpose" option to handle missing or empty cells differently. The "basic" option replaces missing cells with an empty matrix, while the "transpose" option transposes the data and replaces missing cells with "NaN".

4. Can I read data from an Excel file with mixed data types in Matlab?

Yes, you can use the "xlsread" function to read data from an Excel file with mixed data types in Matlab. By default, the function will attempt to convert the data to a numerical array. However, you can use the "text" option to read the data as a cell array of character vectors.

5. How do I import data from an Excel file into a table in Matlab?

To import data from an Excel file into a table in Matlab, you can use the "readtable" function. This function takes in the name of the Excel file and the range of cells to be read as inputs, and returns the data as a table. You can also use the "detectImportOptions" function to customize the import options for your table, such as specifying the variable names and data types.

Similar threads

Replies
18
Views
5K
Replies
5
Views
8K
Replies
4
Views
6K
Replies
4
Views
3K
Replies
4
Views
7K
Back
Top