xlsread isn't storing all the cell values from a column

I've been using xlsread for a while now, and I've never had Any problems with it.
Recently, however, whenever I try to import values from an excel sheet, each column having more than one cell (19 rows),
for some reason, only a single, unrelated value is stored in the variable created for that column.
Funnily enough, when i tried using readcell for one of the values (p4), I found that p1, p2 and p3 were saved appropriately, as 18 * 1 matrices.
(bottom right corner)
When I reverted back to xlsread for p4, p1,p2, p3 and p4 were again saved as single nonsensical values, although the plot containing p4 had the correct values.
(bottom right corner)
I've never had a problem with xlsread before, so could anyone give me some insight into what is happening?

3 Comments

Post the Excel file. There may be something in it that xlsread doesn’t like.
This is the third sheet from the excel file that I'm trying to import.
The first column (THETA) is being imported as a 18*1 array, but the next four columns aren't being imported similarly.
I'd suggest actual code instead of screenshots of code. It's a lot easier to work with that. Also attaching a real file so people can try the solution would help out.

Sign in to comment.

 Accepted Answer

Try something like this —
T1 = readtable('YourExcelFile.xlsx', 'Range','A1:E19')
That will import everything as a table, and then refer to the individual variables as —
THETA = T1.Theta;
and so for the rest, although you can simply refer to the individual variables everywhere using their table references.
See the documentations ection on Access Data in Tables for details.

6 Comments

I tried using what you suggested, but I need these columns to be seperate arrays in the workspace so that I can plot them using cftool.
I don't know if It's possible to plot the column data from the excel sheet in cftool if it's saved as a table, and not individual variables as I used to previously.
‘I tried using what you suggested, but I need these columns to be seperate arrays in the workspace so that I can plot them using cftool.’
It is straightforward to use the separate columns (table variables) in your code.
I even demonstrated how to do it!
‘I don't know if It's possible to plot the column data from the excel sheet in cftool if it's saved as a table, and not individual variables as I used to previously.’
I am certain that it is. I cannot demonstrate that unless you attach your Excel file so that I can work with it.
.
Here it is! Sorry if my doubts are a bit simplistic, I'm fairly new to matlab.
Reading them and plotting them is straightforward using readtable.
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/608465/peaks_tes.xlsx')
T1 = 18×5 table
Theta p1 p2 p3 p4 _____ __________ ________ ______ _____ 10 -0.005575 0.06977 -1.995 10.04 20 -0.001224 0.008275 -2.632 19.93 30 -0.001795 0.05759 -2.309 29.09 40 -0.0003153 0.01896 -2.139 39.02 50 -0.0001241 0.01393 -2.247 49.36 60 -0.0004319 0.02902 -2.788 59.22 70 -0.000556 0.04196 -2.949 69.45 80 -0.0003597 0.03595 -3.183 79.28 90 -0.00043 0.03994 -3.22 89.17 100 -0.0006191 0.05871 -3.777 98.73 110 -0.0005763 0.05886 -4.01 109.8 120 -0.0006326 0.06686 -4.348 119.2 130 -0.0008648 0.08922 -4.972 127.7 140 -0.0006054 0.07392 -4.791 137.5 150 -0.001048 0.1127 -5.909 144.7 160 -0.001076 0.1206 -6.364 153.9
VarNames = T1.Properties.VariableNames;
figure
for k = 1:4
subplot(4,1,k)
plot(T1.Theta, T1{:,k+1})
ylabel(VarNames{k+1})
grid
xlabel('\theta')
end
Refer to them as I referred to them in the plot calls to use them in other parts of your code for further processing.
I have no idea what curve fitting or parameter estimation techniques you want to use, however that should not be a problem now that they are in the workspace.
.
Thank you! I will check it out and you all have been most helpful!

Sign in to comment.

More Answers (2)

I don't know if this is the problem in your case, but Matlab docs have not recommended the use of xlsread since R2019a. That's not to say it doesn't work in most cases, but there are clearly some known issues with its use

2 Comments

It can't be the problem because I've been using it for half a year so far. No problems until today.
The fifth line is a call to readcell, not xlsread. And the error says, error using readcell.

Sign in to comment.

The issue is in the call to readcell which isn't using the correct syntax. See readcell documentation for details.
It looks like you've tried to port an xlsread call to readcell withought modifying the inputs to match what readcell expects
[~,~,C] = xlsread(file,sheet,range);
becomes:
C = readcell(file,"Sheet",sheet,"Range",range);
addendum:
You probably want readmatrix actually
A = xlsread(file,sheet,range);
becomes:
A = readmatrix(file,"Sheet",sheet,"Range",range);

3 Comments

Thanks, I will check out the readcell documentation, it's just, xlsread was a pretty simple way to load excel
data into my code, and use it however I wanted.
readcell notwithstanding, I can't seem to save those columns as variables.
readtable hasn't helped me load these variables into cftool either.
So I tried using that, and the following ocurred:
A = readmatrix('ALL_PEAKS_2ND_SET.xlsx',"Sheet",'Poly3 for 10s angles vs time',"Range",'A2:A19');
Sheet name 'Poly3 for 10s angles vs time' does not exist or is
not supported. To check if the sheet is supported, specify the
sheet by its worksheet index.
From the example file, it looks like there is only "Sheet1" and no "Poly3 for 10s angles vs time"
The error message is suggesing "specify the sheet by its worksheet index." meaning use a number instead of the name.

Sign in to comment.

Categories

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!