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)
Show older comments
Krishna Prasad Rao
on 11 May 2014
Commented: Krishna Prasad Rao
on 26 May 2014
[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
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.
Accepted Answer
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.
More Answers (5)
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
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.
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
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.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!