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.
Ältere Kommentare anzeigen
[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 Kommentare
Star Strider
am 11 Mai 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
am 12 Mai 2014
Bearbeitet: Krishna Prasad Rao
am 12 Mai 2014
Akzeptierte Antwort
Weitere Antworten (5)
Image Analyst
am 12 Mai 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 Kommentare
Krishna Prasad Rao
am 12 Mai 2014
Image Analyst
am 12 Mai 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.
Krishna Prasad Rao
am 12 Mai 2014
Krishna Prasad Rao
am 12 Mai 2014
0 Stimmen
... 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 Kommentar
Krishna Prasad Rao
am 14 Mai 2014
Bearbeitet: Krishna Prasad Rao
am 14 Mai 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 Kommentare
Krishna Prasad Rao
am 16 Mai 2014
dpb
am 16 Mai 2014
How are you creating the Excel sheets? Something's odd in that I couldn't turn the values into numeric using Ctrl+1 to access the "format cells" to set as numeric. It didn't manage to change the content to numeric from text which is what's happening when you import.
Those values that don't have the units labels on them, however, ARE valid numeric and while it shouldn't be necessary you CAN scan them directly with a '%f' format string and the 'E' will be interpreted correctly.
Krishna Prasad Rao
am 26 Mai 2014
dpb
am 26 Mai 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.
Krishna Prasad Rao
am 26 Mai 2014
Kategorien
Mehr zu Text Data Preparation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!