Matlab: Reading in data from an excel spreadsheet as a single integer

I am reading in data from an excel spreadsheet, but I am unable to use a comparison on what I read in. How can I read in a cell from an excel spreadsheet and compare what I get to a number? I'm pretty sure anything read from a spreadsheet is made into an array Here is the relevant code (edit: I'll give the whole code of what I have; edit2: Minor fixes, but being so new to MatLab, I think I'm not familiar with how to declare a variable):
>> for k = 2:4997
colF = 'F';
colE = 'E';
row = int2str(k);
entryF = strcat(colF, row);
entryE = strcat(colE,row);
millisecond = xlsread('someFile.xlsx', 1, entryE);
[~,message] = xlsread('someFile.xlsx', 1, entryF);
if millisecond == 1
soundMoment = 0;
elseif strcmp(message, 'probe_sound')
soundMoment = millisecond;
end
if soundMoment == 0
xlswrite('someFile.xlsx', 'preprobe', 1, entryF);
elseif millisecond > soundMoment
xlswrite('someFile.xlsx', 'postprobe', 1, entryF);
end
end

25 件のコメント

Walter Roberson
Walter Roberson 2011 年 12 月 22 日
Please check your file names: probably you should not have the * in the first one.
You should be using strcmp(message, 'probe_sound') instead of using == to compare the strings.
Aldin
Aldin 2011 年 12 月 22 日
You say entryE is number an entryF is text than you can in my opinion make this compare:
if entryE = 1
soundMoment = 0;
elseif strcmp(entryF,'probe_sound')
soundMoment = millisecond;
end
Aldin
Aldin 2011 年 12 月 22 日
The star and backslash is not needed. just type xlsread('someFile.xlsx',1,entryE)
Walter Roberson
Walter Roberson 2011 年 12 月 22 日
EntryE and EntryF are locations in the file, not the entries themselves.
The "=" operator cannot be used in an "if" statement; "==" is the comparison operator.
Aldin
Aldin 2011 年 12 月 22 日
"good eye" :)
read data in excel and than xlswrtie the same data and than compare
what you want
Louis
Louis 2011 年 12 月 22 日
The error I get in MatLAB is:
Error using == , Matrix dimensions must agree.
So I know the problem is with what I read in, the only issue is WHAT the datatype is of millisecond and message. Do I just need to convert message into a string, and millisecond into an integer?
Aldin
Aldin 2011 年 12 月 22 日
1 is an number you have to convert millisecond to number "str2num"
also for soundMoment
Louis
Louis 2011 年 12 月 22 日
So anything read in from excel is a string?
Aldin
Aldin 2011 年 12 月 22 日
NO
if you type the code below your variable "name" is string
Louis
Louis 2011 年 12 月 22 日
I don't follow. Does the ~ in [~,name] stand for something? And name is just what I want my variable to be named?
Aldin
Aldin 2011 年 12 月 22 日
It is your variable . In your case : "message" see bellow
Louis
Louis 2011 年 12 月 22 日
No error with the [~,message] bit, but now it says: Error using str2num (line 33), Requires string or character array input.
Walter Roberson
Walter Roberson 2011 年 12 月 22 日
In 2009b onward, a ~ on the left-hand side of an assignment means to throw away the corresponding output argument. If you are using a version before 2009b, replace the ~ with any variable name that is not being used.
Aldin
Aldin 2011 年 12 月 22 日
delete all str2num functions in your code. And what is preprobe and postprobe it is the data to read in file???
Aldin
Aldin 2011 年 12 月 22 日
and declare soundMoment before if statement!!!!
Aldin
Aldin 2011 年 12 月 22 日
and put so in your code 'preprobe' and 'postprobe' And than it works
by me :)
Louis
Louis 2011 年 12 月 22 日
Oops, preprobe and postprobe are supposed to be strings that I'm writing into that spot in the Excel spreadsheet. I thought in MatLAB that declarations weren't needed.
Aldin
Aldin 2011 年 12 月 22 日
What's going now with your code :)
Louis
Louis 2011 年 12 月 22 日
Did another small edit to the code, but I'm assuming to declare soundMoment, int soundMoment should work?
Walter Roberson
Walter Roberson 2011 年 12 月 22 日
Declarations are not required. The problem is that your conditions do not specify what soundMoment should be set to if neither test is met.
Louis
Louis 2011 年 12 月 22 日
I did "else soundMoment = -1;" and now there are no errors. The excel spreadsheet is quite big, so I'll have to wait to see if it came out as intended.
Louis
Louis 2011 年 12 月 23 日
It works, except it's never listing anything as preprobe. I get all postprobe right, but preprobe isn't showing.
Louis
Louis 2011 年 12 月 23 日
It works as intended, I made a small error in the file name.
Aldin
Aldin 2011 年 12 月 23 日
In MATLAB there is no declaration such as in JAVA we have int, float, double, string...
Aldin
Aldin 2011 年 12 月 23 日
Did that do what you want? If so, mark it as "solved."

サインインしてコメントする。

回答 (2 件)

Aldin
Aldin 2011 年 12 月 22 日

0 投票

Here is the right code for getting data from excel doc.
[~,name] = xlsread('test.xlsx',1,'A1')
Aldin
Aldin 2011 年 12 月 22 日

0 投票

Edit your code with this:
[~,message] = xlsread('someFile.xlsx', 1, entryF);
your code seems to be OK

質問済み:

2011 年 12 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by