xlsread unable to import data or sorts the imported data. please advice.

I am trying to read x and y coordinates into separate arrays, say x and y. Sometimes,
  1. xlsread does not read any value from the specified column
  2. xlsread grabs the value from the column but sorts them resulting in mismatch between x and y coordinates
I am not sure whats the way around? Is it a bug? It happens sometimes only.

1 comentario

Image Analyst
Image Analyst el 17 de Abr. de 2015
Editada: Image Analyst el 17 de Abr. de 2015
It could be a weird entry in your Excel cell. Who knows? Not us, because you didn't attach a workbook for which it fails for us to check.

Iniciar sesión para comentar.

 Respuesta aceptada

Nitin, I was able to load this 20 times in a row without issue. But, I am on Mac, which uses quite a different code path than a Windows computer (you didn't say what OS you are using), so we may not be making much of a comparison.
Try not to have Excel running on your computer at the same time you run this code. I've heard claims, but never personally verified, that reliability drops when Excel is being used both as a Desktop application and Automation server.
This is a stab in the dark, but try loading the spreadsheet in one command, passing 'basic' as the fourth argument to xlsread. This will cause MATLAB to read the file with its own reader, instead of bridging to Excel using Automation to do the read. The assign the columns to individual variable. Maybe there is some kind of timing issue with running xlsread in rapid succession? That code would look like:
foo=xlsread('test1.xlsx','Sheet1','','basic');
ya=foo(:,1);
za=foo(:,2);
yb=foo(:,28);
zb=foo(:,29);

1 comentario

nitin arora
nitin arora el 27 de Abr. de 2015
Editada: nitin arora el 27 de Abr. de 2015
Thanks Ken, I did not get into the problem of xlsread sorting my data again. you are right that excel was open when I was trying to import the data from excel into matlab.
Thanks image analyst.

Iniciar sesión para comentar.

Más respuestas (1)

I can answer question #2, I think. If you have two separate variables and you sort one, then, yes, the two variables will now be out of sync. To can overcome this by:
  1. Use a container like a table that keeps columns of related data synched.
  2. Use the second output of sort and apply that to the second array:
[X, I] = sort(X);
Y = Y(I);

6 comentarios

He said xlsread() sorts them. Why would it do that?
Oh, I missed that. I don't have an explanation then (and I know this code to some degree). Nitin, you'll really need to upload a spreadsheet that demonstrates the problem.
Maybe he called xlsread() twice - once for each column - and then actually did call sort() on his own. I guess we'll have to see his actual code and workbook to know for sure.
Thanks. Here is the attached spreadsheet and code. This one does not sort the array but is unable to pick up the data. As it turns out, I restarted my office desktop and problem went away for a while. Same problem with my personal laptop. Really scary!
clc
clear all
xa=xlsread('test.xlsx','Sheet1','A2:A6');
ya=xlsread('test.xlsx','Sheet1','B2:B6');
xb=xlsread('test.xlsx','Sheet1','AV2:AV6');
yb=xlsread('test.xlsx','Sheet1','AW2:AW6');
Please attach test.xlsx. You probably just clicked the "Choose file" button but forgot to click the "Attach file" button. And just to be clear, you're saying that it is the xlsread() itself that is rearranging/sorting the cells and you are definitely not calling sort() yourself, right?
Sorry. Please find ignore the previous message and code. Yes, xlsread() sorted my data yesterday without using any sort command. Attached code is not sorting the data today but it was doing that yesterday. After restarting the desktop, the problem went away.
If I saw this sorting behavior again, I will let you know.However, attached code is unable to pick some columns. Please run the code several times. Sometimes, the value gets picked but sometimes it does not. Seems like a bug.
clc
clear all
ya = xlsread('test1.xlsx','Sheet1','A3:A7'); za = xlsread('test1.xlsx','Sheet1','B3:B7'); yb = xlsread('test1.xlsx','Sheet1','AB3:AB7'); zb = xlsread('test1.xlsx','Sheet1','AC3:AC7');

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 17 de Abr. de 2015

Editada:

el 27 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by