Matlab: Reading in data from an excel spreadsheet as a single integer
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 22 Dez. 2011
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
am 22 Dez. 2011
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
am 22 Dez. 2011
The star and backslash is not needed. just type xlsread('someFile.xlsx',1,entryE)
Walter Roberson
am 22 Dez. 2011
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
am 22 Dez. 2011
"good eye" :)
read data in excel and than xlswrtie the same data and than compare
what you want
Louis
am 22 Dez. 2011
Aldin
am 22 Dez. 2011
1 is an number you have to convert millisecond to number "str2num"
also for soundMoment
Louis
am 22 Dez. 2011
Aldin
am 22 Dez. 2011
NO
if you type the code below your variable "name" is string
Louis
am 22 Dez. 2011
Aldin
am 22 Dez. 2011
It is your variable . In your case : "message" see bellow
Louis
am 22 Dez. 2011
Walter Roberson
am 22 Dez. 2011
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
am 22 Dez. 2011
delete all str2num functions in your code. And what is preprobe and postprobe it is the data to read in file???
Aldin
am 22 Dez. 2011
and declare soundMoment before if statement!!!!
Aldin
am 22 Dez. 2011
and put so in your code 'preprobe' and 'postprobe' And than it works
by me :)
Louis
am 22 Dez. 2011
Aldin
am 22 Dez. 2011
What's going now with your code :)
Louis
am 22 Dez. 2011
Walter Roberson
am 22 Dez. 2011
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
am 22 Dez. 2011
Louis
am 23 Dez. 2011
Louis
am 23 Dez. 2011
Aldin
am 23 Dez. 2011
In MATLAB there is no declaration such as in JAVA we have int, float, double, string...
Aldin
am 23 Dez. 2011
Did that do what you want? If so, mark it as "solved."
Antworten (2)
Kategorien
Mehr zu Spreadsheets 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!