Hi, i want to plot two columns of values of data from excel, and here is my code but it is saying Index exceeds matrix dimensions.

13 views (last 30 days)
[A,B,C]=xlsread('Extra_try.xlsx','Blad1') ;
x = cell2mat(A(2:462, 1)); % Untested
y = cell2mat(A(2:462, 2)); % Untested
plot(x,y);
  2 Comments
Star Strider
Star Strider on 11 May 2014
First, just do cell2mat(A) and see what the sizes of the resulting matrix are.
Then edit your Question to include that information.
Krishna Prasad Rao
Krishna Prasad Rao on 12 May 2014
Edited: Krishna Prasad Rao on 12 May 2014
Hi thanks for your response, Star Strider it is saying that the size is 0.
dpb the same is the case with your suggestion, the matrix is out of bounds.

Sign in to comment.

Accepted Answer

dpb
dpb on 13 May 2014
Edited: dpb on 13 May 2014
>> [a,b]=xlsread('xl.xls');
>> whos a
Name Size Bytes Class Attributes
a 0x0 0 double
>> whos b
Name Size Bytes Class Attributes
b 462x2 83520 cell
>> b(1:4,:)
ans =
'Frequency' 'Amplitude'
'0.000000Hz' '4.590542dBm'
'6521739.130435Hz' '0.599657dBm'
'13043478.260870Hz' '0.612001dBm'
>>
Well, there's your problem...you've got units on each entry in the two columns of values so they're not numeric and are returned as the text cell array.
It'll be a lot easier if you can create your files without that complication; if not you'll have to convert to numeric internally and there's no ready-built technique to do that automagically as str2double and friends don't know anything about formatting. I'm sure regexp can be brought to bear but I'm not adept with it.
Brute force is sotoo...
>> sscanf([b{2,1} b{2,2}],'%fHz%fdBm').'
ans =
0 4.5905
so one needs must
N=length(b)-1; % number elements w/o header
dat=zeros(N,2); % preallocate
for i=1:N
dat(i,:)=sscanf([b{i+1,1} b{i+1,2}],'%fHz%fdBm').';
end
Then you'll finally have data you can plot and do whatever with.
As said, fix the files to eliminate the Hz and dBm in the columns instead if at all possible. Label the columns in the header if you must.
Illustrates that one needs must supply ALL pertinent info to get good answers...a sample of the file data early on could have saved much time/effort.
  1 Comment
Krishna Prasad Rao
Krishna Prasad Rao on 13 May 2014
Hi dpb! Thanks very much, now i understand how to plot the graphs by eliminating the units, now i want to compare two plotted graphs and i dont know how one does that, is there any specific function or something that one can from the library to compare two graphs or draw both in one and the same graph. Thanks in advance /Krishna

Sign in to comment.

More Answers (5)

Image Analyst
Image Analyst on 12 May 2014
Try getting rid of the sheet name:
[A,B,C]=xlsread('Extra_try.xlsx');
emptyA = isempty(A) % No semicolon
emptyB = isempty(B) % No semicolon
emptyC = isempty(C) % No semicolon
sizeA = size(A) % No semicolon
sizeB = size(B) % No semicolon
sizeC = size(C) % No semicolon
If the size is zero or it's empty, then your workbook is blank.
  2 Comments
Krishna Prasad Rao
Krishna Prasad Rao on 12 May 2014
Hey thnaks Image Analyst!
What do you mean by work book, the excel sheet is not empty if thats what you meant. I tried with your method it still says the same thing, out of bounds cause size of a is 0. I am a beginner in matlab i am trying to read two coloumns of data from excel and plot a graph in matlab based on that.
Image Analyst
Image Analyst on 12 May 2014
A workbook is an Excel file. A workbook may have one or more worksheets in it. A "worksheet" is just the new name Microsoft started using about 15 years ago instead of "spreadsheet" but they're the same thing.

Sign in to comment.


Krishna Prasad Rao
Krishna Prasad Rao on 12 May 2014
Hi thanks to you both again, i have tried the older version of XLs by changing both with the excel dokument and in the code, yet it showed the same result,
Here is my code
[A,B,C]=xlsread('Extra_try.xlsx');
emptyA = isempty(A) % No semicolon
emptyB = isempty(B) % No semicolon
emptyC = isempty(C) % No semicolon
sizeA = size(A) % No semicolon
sizeB = size(B) % No semicolon
sizeC = size(C) % No semicolon
% x = cell2mat(A(2:462, 1)); % Untested
% y = cell2mat(A(2:462, 2)); % Untested
% x = A(2:462, 1);
% y = A(2:462, 2);
% x = cell2mat(A);
% y = cell2mat(A);
x = cell2mat(A(:, 1)); % Untested
y = cell2mat(A(:, 2)); % Untested
plot(x,y);
and here are the errors after compiling
Attempted to access A(:,1); index out of bounds because size(A)=[0,0].
Error in plotButtonCallback (line 17)
x = cell2mat(A(:, 1)); % Untested
i have even attached my excel file here, and i want to plot frequency on the x-axis while
amplitude or Power on y-axis from the first two columns in the excel worksheet.
Thank you!

Krishna Prasad Rao
Krishna Prasad Rao on 12 May 2014
Here is my excel file

dpb
dpb on 13 May 2014
Edited: dpb on 13 May 2014
... or draw both in one and the same graph
doc hold
plot(x1,y1)
hold on
plot(x2,y2)
or,
plot([x1 x2], [y1 y2]) % presumes columns for x,y and commensurate lengths
  1 Comment
Krishna Prasad Rao
Krishna Prasad Rao on 14 May 2014
Edited: Krishna Prasad Rao on 14 May 2014
Thanks dpb!
I tried it but unfortunately iam facing the same problem matrix index but this time for Another file and i tried to eliminate the text but instead i need to multiply after certain no. of rows with 10 and plot the both the graphs in one and the same but as said it is giving me the error index exceeds matrix dimensions.
Here is my script
[A,B,C]=xlsread('Signalledare.xlsx');
N=length(B)-1; % number elements w/o header
dat=zeros(N,2); % preallocate
for i=1:N
dat(i,:)=sscanf([B{i+1,1} B{i+1,2}],'%fHz%fdBm').';
end
x1 = (dat(:, 1)); % Untested
y1 = (dat(:, 2)); % Untested
[D,E,F] = xlsread('ADS_signalledare.xlsx');
O = length(E)-1;
dat1 = zeros(O,2);
for j=1:175
dat1(j,:)=sscanf([E{j+1,1}],'%E8').';
end
for j=176:O
dat1(j,:)=sscanf([E{(j+1)*10,1}],'%E9').';
end
for j=1:199
dat1(j,:)=sscanf([E{j+1,2}],'%E-2').';
end
for j=200:O
dat1(j,:)=sscanf([E{(j+1)*10,2}],'%E-1').';
end
x2 = (dat1(:, 1)); % Untested
y2 = (dat1(:, 2)); % Untested
doc hold
plot(x1,y1);
hold on
plot(x2,y2);
and here is the error in command window
Index exceeds matrix dimensions.
Error in vinayaka_signalledare (line 16)
dat1(j,:)=sscanf([E{(j+1)*10,1}],'%E9').';
but when i remove the multiplying factor of 10 it works just fine to produce one graph and the help window shows up defining hold command and i am not able to figure out if this produced figure is double graph or single graph.
Moreover i am not able to use Another command plot ([x1 x2], [y1 y2]) cause the two matrices are not consistent, i.e., i cannot concatenate due to different sizes of matrices.
I am attaching both my excel files here for reference. Thanks in advance!
/Krishna

Sign in to comment.


dpb
dpb on 14 May 2014
Edited: dpb on 14 May 2014
dat1(j,:)=sscanf([E{(j+1)*10,1}],'%E9').';
In
[E{(j+1)*10,1}]
you've multiplied the index by 10, not the value; you're just reading the value at that point.
dat1(j,:)=10*sscanf([E{j+1,1}],'%E9').';
You don't want the '9' in the format string, however, that's going to match the "E" in the string and discard it, not use it as the exponent so you'll mess up the magnitude. Just use either '%e' or '%f', either will scan a floating point value correctly.
BTW, since you did fix the columns to not include the units on the second file you should now find that D has the data already in numeric form so you don't need to do the conversion internally; just use it.
However, I'm unable to make a numeric format "stick" on a copy of your spreadsheet--I don't know how you must have created it or what's set that seems to be preventing doing so; I'm very inexperienced with Excel. I could copy and paste a cell elsewhere on the sheet and it would turn to numeric, but the 'Format Cells' function seemed powerless to modify the existing columns.
I'd strongly suggest that for ease of use you figure out what's going on there and fix it so you can read the data directly as numeric instead of as text. It's peculiar; a symptom I've never encountered before so I don't know what the deal is on that.
Also, if you would just set a range for the values you wish to multiply, you can do it w/o a loop--"the Matlab way" and a prime reason Matlab is so useful.
O = length(E)-1;
dat1 = zeros(O,2);
for j=1:175
dat1(j,:)=sscanf([E{j+1,1}],'%E8').';
end
for j=176:O
dat1(j,:)=sscanf([E{(j+1)*10,1}],'%E9').';
end
for j=1:199
dat1(j,:)=sscanf([E{j+1,2}],'%E-2').';
end
for j=200:O
dat1(j,:)=sscanf([E{(j+1)*10,2}],'%E-1').';
end
Oh, I see the above problem on the scanning is what you're trying to fix by the "multiply by 10". The correct answer is to fix your spreadsheet to save the values as numeric and then read them directly, not have to try to fixup a problem introduced by that they are, for some reason, stuck as text.
  5 Comments
dpb
dpb on 26 May 2014
Ah, so...as far as I know, indeed, all Matlab knows is 'dot' for decimal.
plot will only draw the values it's given; it doesn't have any facility to do operations on data or between different plots builtin. So, fundamentally, the answer to your question is "yes, that's the only way". You can, of course, dispense with the actual array and write the operation as the argument like
plot(x,y1-y2)
to accomplish it. There are, of course, many variations upon that theme.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!